Python File Directory
File, Directory
File and Directory operations are general topic for programming.
os package has a lot of feature for them
import os
Get current working directory
print(os.getcwd()) # working directory print(os.getcwdb()) # byte
Second one is b” style(binary)
List directory
print(os.listdir()) print(os.listdir(os.getcwd()))
Create directory
os.mkdir('test') os.rename('test', 'test2')
Exists
print(os.path.exists(os.getcwd() + '/test2')) print(os.path.isdir(os.getcwd() + '/test2')) # Directory or not print(os.path.isfile(os.getcwd() + '/test2')) # File or not
Path
print(os.path.dirname(os.path.realpath(__file__)))
realpath