-
Notifications
You must be signed in to change notification settings - Fork 0
/
os-module-methods.txt
43 lines (32 loc) · 1.13 KB
/
os-module-methods.txt
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
39
40
41
42
43
# importing os, not needed to be installed explicitly
import os
# all the attributes & methods of os
print(dir(os))
print(dir(os.path))
# get current directory
print(os.getcwd())
# change directory
os.chdir('path of the new folder')
# list of all files & folders in current directory
os.listdir()
os.listdir('path')
# create directory
os.mkdir('directory name')
os.makedirs('directory name')
# if you want to create directory a few levels deep, then makedirs will create the intermediate directories for you
# delete file or folder
os.rmdir('file path')
os.removedirs('file path')
# renaming file or folder
os.rename('original file name', 'new file name')
# information about a particular file
print(os.stat('file name'))
# if we want to see the entire directory tree and files within our current directory
for dirpath, dirnames, filenames in os.walk('path') :
print('Current Path', dirpath)
print('Current Path's folder names', dirnames)
print('Current Path's file names', filenames)
# concatenationg file path
os.path.join('path1', 'path2')
# checking if a file exists or not
os.path.exists('filename')