-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter8_file_read_write.py
More file actions
62 lines (46 loc) · 1.73 KB
/
Copy pathchapter8_file_read_write.py
File metadata and controls
62 lines (46 loc) · 1.73 KB
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
import os, shutil
def file_open():
file = open('data/input.txt', 'r')
return file
def use_shutil():
if os.path.exists(r'C:\Users\default.DESKTOP-9K8MUSK\Documents\dev\test_workspace\eggs.txt'):
shutil.move(r'C:\Users\default.DESKTOP-9K8MUSK\Documents\dev\test_workspace\eggs.txt', r"C:\Users\default.DESKTOP-9K8MUSK\PycharmProjects\python_automation\practical_python\bacon.txt")
def use_os_walk():
for folderName, subFolders, fileNames in os.walk(os.getcwd()):
print('current folder is '+folderName)
for subFolder in subFolders:
print('SubFolder of '+folderName+':'+subFolder)
for fileName in fileNames:
print('File inside'+folderName+':'+fileName)
print('\n')
def moveAnotherFileName():
#date pattern
src = os.path.join(os.getcwd(), 'bacon.txt')
dst = os.path.join(os.getcwd(), 'date.txt')
moveFile(src, dst)
def moveFile(src, dst):
if os.path.exists(src):
shutil.move(src, dst)
if __name__ == '__main__':
# 현재 경로 얻어오기 getcwd
current_path = os.getcwd()
print(current_path)
print(os.path.abspath(current_path))
# 폴더 만들기
if os.path.exists(r'C:\Users\default.DESKTOP-9K8MUSK\Documents\dev') \
and not os.path.exists(r"C:\Users\default.DESKTOP-9K8MUSK\Documents\dev\test_workspace"):
os.makedirs(r"C:\Users\default.DESKTOP-9K8MUSK\Documents\dev\test_workspace")
# join
print(os.path.join('usr', 'bin', 'spam'))
# 분해하기
print(current_path.split(os.path.sep))
#shutil 사용해보기
use_shutil()
#use os walk()
use_os_walk()
#move file name
moveAnotherFileName()
# 파일 open
f = file_open()
print(f.readline())
f.close()