-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprog48_OS_Module.py
68 lines (41 loc) · 1.59 KB
/
prog48_OS_Module.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Os Module
import os
#print(dir(os)) # print all OS module attributes
# ---- current working directory (cwd) -----
print(os.getcwd())
# ---- Change current working directory ----
# os.chdir("D://")
# print(os.getcwd())
# ---- list of all files in cwd --
#print(os.listdir())
# ---- list of all files of any drive --
print(os.listdir("d://"))
print(os.path.splitext("ram.txt")) # split the file and extension return in tuple
print(os.path.splitext("ram.txt")[1])
# --- create folder -----
#os.mkdir("ram")
# --- create folder within folder
#os.makedirs("Ram/Shyam") # this will create two folders, shyam folder inside ram folder
# --- rename files ----
#os.rename("mylog.txt","log.txt")
# ---- get environment variables -------
#print(os.environ.get("Path"))
# ----- join path --------
pth = os.path.join("D://", "/ram.txt")
print(pth)
# --- check exist path ----
print(os.path.exists("E://ram"))
# --- check file folder ----
# print(os.path.isfile("D:\Python Practice\CODE_WITH_HARRY\Python\log.txt"))
# print(os.path.isdir("D:\Python Practice\CODE_WITH_HARRY\Python"))
# ---
datapath = r"D:\Users\RAM\Desktop\StateName08-02-19_23_09_13\StateName08-02-19_23_09_13"
onlyfiles = []
# for item in myfile:
# if os.path.isdir(os.path.join(datapath,item)):
# onlyfiles.append(os.listdir(os.path.join(datapath,item)))
# print(onlyfiles)
for path, subdirs, files in os.walk(datapath): # -- loop in folder subfolders
for name in files:
# onlyfiles.append(name)
onlyfiles.append(os.path.join(path, name))