This is a draft cheat sheet. It is a work in progress and is not finished yet.
OS Module
os.sep |
gives the current working directory as the sting value. |
os.getcwd() |
gives the current working directory as the sting value |
os.chdir("Dir loc") |
takes in directory location and changes the working directory |
os.listdir("filename") |
gives s the list of all the files. |
os.makedirs("foldernames") |
creates the folders. |
os.unlink("filename") |
deletes a file |
os.rmdir("folder") |
deletes a folder. |
os.walk("folder name") |
this returns foldername, subfolders and files in it |
for foldername, subfoldername, filename in os.walk("."):
... print(foldername, subfoldername, filename)
shutil
shutil.copy("source", "destination") |
Copies source file to destination file |
shutil.copytree("source_folder", "destination_Folder") |
useful for backups |
shutil.move("source_folder", "destination_folder") |
This can also be used as renaming option. |
shutil.rmtree("dir_name") |
To delete a full directory we use this |
os.path module
os.path.join ("folder1", "folder2", "folder3", "filename") |
This takes the folder list and joins them to give a path name |
os.path.abspath("filename") |
this gives the absolute path of the file |
os.path.isabs("filepath") |
gives true of false for the abs path |
os.path.relpath("filepath") |
gives relative path for the 2 paths you give. |
os.path.exists("Filename") |
gives true or false for the file name |
os.path.isfile("filepath") |
true if the path is file |
os.path.isdir("filepath") |
true if the path is folder |
os.path.getsize("filename") |
gives the size in bytes |
|
|
|