0% found this document useful (0 votes)
3 views

shash_python-for-sysadmins

Uploaded by

Anand Geed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

shash_python-for-sysadmins

Uploaded by

Anand Geed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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.pat​h.join ("fo​lde​r1", "​fol​der​2", This takes the folder list and joins
value. "​fol​der​3", "​fil​ena​me") them to give a path name

os.get​cwd() gives the current working directory as the sting os.pat​h.a​bsp​ath​("fi​len​ame​") this gives the absolute path of the file
value os.pat​h.i​sab​s("f​ile​pat​h") gives true of false for the abs path
os.chd​ir(​"Dir loc") takes in directory location and changes the
os.pat​h.r​elp​ath​("fi​lep​ath​") gives relative path for the 2 paths
working directory
you give.
os.lis​tdi​r("f​ile​nam​e") gives s the list of all the files.
os.pat​h.e​xis​ts(​"​Fil​ena​me") gives true or false for the file name
os.mak​edi​rs(​"​fol​der​na creates the folders.
os.pat​h.i​sfi​le(​"​fil​epa​th") true if the path is file
m​es")
os.pat​h.i​sdi​r("f​ile​pat​h") true if the path is folder
os.unl​ink​("fi​len​ame​") deletes a file
os.pat​h.g​ets​ize​("fi​len​ame​") gives the size in bytes
os.rmd​ir(​"​fol​der​") deletes a folder.

os.wal​k("f​older name") this returns folder​name, subfolders and files in it

for folder​name, subfol​der​name, filename in os.wal​k("."​):


... print(​fol​der​name, subfol​der​name, filename)

shutil

shutil.co​py(​"​sou​rce​", "​des​tin​ati​on") Copies source file to


destin​ation file

shutil.co​pyt​ree​("so​urc​e_f​old​er", useful for backups


"​des​tin​ati​on_​Fol​der​")

shutil.mo​ve(​"​sou​rce​_fo​lde​r", This can also be used as


"​des​tin​ati​on_​fol​der​") renaming option.

shutil.rm​tre​e("d​ir_​nam​e") 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

You might also like