1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import os
print(os.name) print(os.environ)
print(os.getcwd()) print(os.listdir('.')) print(os.stat('test.txt'))
os.rename('test.txt', 'test.py') os.remove('test.py')
os.mkdir('test') os.chdir('test') os.rmdir('test') os.makedirs('dir1/dir2', exist_ok=True)
for root, dirs, files in os.walk('.'): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name))
names = [name for name in os.listdir('.') if os.path.isfile(os.path.join('.', name))] print(names)
dirnames = [name for name in os.listdir('.') if os.path.isdir(os.path.join('.', name))] print(dirnames)
txtfiles = [name for name in os.listdir('.') if name.endswith('.txt')] print(txtfiles)
|