Python For Sysadmins Cheat Sheet
by shash via cheatography.com/18578/cs/15129/
OS Module os.path module
os.sep gives the current working directory as the sting os.path.join ("folder1", "folder2", This takes the folder list and joins
value. "folder3", "filename") them to give a path name
os.getcwd() gives the current working directory as the sting os.path.abspath("filename") this gives the absolute path of the file
value os.path.isabs("filepath") gives true of false for the abs path
os.chdir("Dir loc") takes in directory location and changes the
os.path.relpath("filepath") gives relative path for the 2 paths
working directory
you give.
os.listdir("filename") gives s the list of all the files.
os.path.exists("Filename") gives true or false for the file name
os.makedirs("folderna creates the folders.
os.path.isfile("filepath") true if the path is file
mes")
os.path.isdir("filepath") true if the path is folder
os.unlink("filename") deletes a file
os.path.getsize("filename") gives the size in bytes
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", useful for backups
"destination_Folder")
shutil.move("source_folder", This can also be used as
"destination_folder") renaming option.
shutil.rmtree("dir_name") To delete a full directory we use
this
By shash Not published yet. Sponsored by CrosswordCheats.com
cheatography.com/shash/ Last updated 26th March, 2018. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com