Eventually you will need to use the filesystem at somepoint, this means creating files and directories and obtaining information about them, this is one area where operating systems do differentiate, directories structures between windows and linux are different. Older versions of Python used functions os and os.path, newer versions (=>3.5) use a new library called pathlib which uses a OOP approach, i will include examples of both. I will really only list some examples and wont go into too much details as they will be very obvious, just to recap I want to explain the two types of paths
Absolute | #### Windows C:\Program Files\Doom D:\backup\June #### Linux/Mac (note the leading slash) /bin/Doom /floppy/backup/June /Applications/Utilities |
Relative | #### Windows mydata\project1\readme.txt games\tetris #### Linux/Mac mydata/project1/readme.txt games/tetris Utilities/Java |
Below is a quick list of commands for both os and pathlib for actions on directories, there are many more see documentation for full list
Operation | os | pathlib |
---|---|---|
import module | import os | import pathlib |
Get working directory | os.getcwd() | pathlib.Path() |
List a directory | os.listdir(<folder name>) | for x in pathlib.Path().iterdir(): print(x) |
Change to a directory | os.chdir("../testing") | n/a |
Create a path name | os.path.join('bin', 'utils', 'disktools') Note: will create bin\utils\disktools |
pathlib.Path().joinpath('bin', 'utils', 'disktools') Note: will create bin\utils\disktools |
Create a directory | os.mkdir('testing') os.makedirs('testing/test1') Note: will create bin\utils\disktools |
new_path = Path ('mydir') new_path.mkdir(parents=True) |
Remove a directory | os.rmdir('testing') Note: will create bin\utils\disktools |
new_path = Path('mydir') new_path.rmdir() |
Is path a directory | os.path.isdir(os.curdir) Note: returns True or False |
pathlib.Path().is_dir() Note: returns True or False |
Scan a directory | with os.scandir(".") as my_dir: for entry in my_dir: print(entry.name, entry.is_file()) Note: returns an iterator of directory objects, can be faster than os.listdir() |
p = Path('C:\\Users\\akrio\\Desktop\\Test').glob('**/*') files = [x for x in p if x.is_file()] |
Walk a directory | for root, dirs, files in os.walk(os.curdir): print("{0} has {1} files".format(root, len(files))) if ".git" in dirs: dirs.remove(".git") |
see above |
Below is a quick list of commands for both os and pathlib for actions on files, there are many more see documentation for full list
Operation | os | pathlib |
---|---|---|
does file exist | os.path.exists('C:\\test.txt') Note: you can also use this to test if a directory exists |
file = pathlib.Path().joinpath('Test.py') print( file.exists() ) |
is this a file | os.path.isfile('C:\\test.txt') | file = pathlib.Path().joinpath('Test.py') print( file.is_file() ) |
rename file (or directory) | os.rename('< original name >','< new name >') | old_path = Path('registry.bkp') new_path = Path('registry.bkp.old') old_path.rename(new_path) Note: you could obmit the variables |
delete a file | os.remove('C:\\test.txt') | new_path = Path('book1.doc.tmp') new_path.unlink() |
Split path into base and file name (including extension) | os.path.split('C:\\testing\test.txt') os.path.splittext(<path>) os.path.basename(<path>) |
pathlib.Path().joinpath('../testing/', 'test.txt').parts Path.suffix Path.name |
Get file information | os.stat('Test.py') os.stat('Test.py').st_uid # get specific info |
Path('somefile.txt') Path('somefile.txt').stat() |
Get file size | os.path.getsize('C:\\test,txt') | Path('somefile.txt').stat().st_size |
Below is a quick list of commands for obtaining operating system information, there are many more see documentation for full list
Operation | os | pathlib |
---|---|---|
Get platform info | os.name | n/a |
Maps the environment | os.environ | n/a |