ํŒŒ์ผ ๊ฒฝ๋กœ ์ด๋™(copy, move)ํ•˜๊ณ  ์‚ญ์ œ(rm)ํ•˜๊ณ  ์••์ถ•(zip)ํ•˜๊ธฐ
Programming/Python

ํŒŒ์ผ ๊ฒฝ๋กœ ์ด๋™(copy, move)ํ•˜๊ณ  ์‚ญ์ œ(rm)ํ•˜๊ณ  ์••์ถ•(zip)ํ•˜๊ธฐ

1. shutil ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ - copy(),  move() 

>>> import shutil, os

>>> os.chdir('/Users/eunhye/Desktop/Workspace')

#shutil.copy() : source๋ฅผ destination์— ๋ณต์‚ฌ
#shutil.copy(source, destination)
>>> shutil.copy('A.txt', './Test')
'./Test/A.txt'

#destination ์ด๋ฆ„์€ ์œ ์ง€๋˜๊ณ  ๋‚ด์šฉ์ด source ๋ณต์‚ฌ
>>> shutil.copy('B.txt', './Test/AA.txt')
'./Test/AA.txt'

 

#shutil.copytree() : ๋””๋ ‰ํ† ๋ฆฌ ํ†ต์ฑ„๋กœ ๋ณต์‚ฌ
>>> shutil.copytree('.', '../Workspace_Backup')
'../Workspace_Backup'

#shutil.move() : source๋ฅผ destination์— ์ด๋™
#shutil.move(source, destination)
>>> shutil.move('C.txt', './Workspace')
'./Workspace/C.txt'

#๋™์ผํ•œ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฎ์–ด์“ฐ๊ธฐ
#๋™์ผํ•œ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด New ํŒŒ์ผ ์ด๋ฆ„ ์ง€์ •ํ•ด์ค˜์•ผ ํ•จ
>>> shutil.move('C.txt', './Workspace/New_C.txt')
'./Workspace/New_C.txt'

 

2. shutil &os ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ - unlink(), rmdir(), rmtree() 

>>> os.chdir('./Workspace')

#os.unlink() : ํŒŒ์ผ ์‚ญ์ œ
>>> os.unlink('New_C.txt')

#os.rmdir() : ๋””๋ ‰ํ† ๋ฆฌ ์‚ญ์ œ
#๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์‚ญ์ œ ์•ˆ๋จ
>>> os.rmdir('./Test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 66] Directory not empty: './Test'

#shutil.rmtree() : ๋””๋ ‰ํ† ๋ฆฌ ์‚ญ์ œ
#๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์‚ญ์ œ
>>> shutil.rmtree('./Test')

#find(), startswith(), endswith()์„ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ„ ํŒŒ์ผ์„ ์ง€์šธ ์ˆ˜ ์žˆ์Œ

>>> os.chdir('..')

#os.walk() : foldername, subfolders, filenames ๋ฐ˜ํ™˜
>>> for foldername, subfolders, filenames in os.walk('.'):
...     print('folder name : ' + foldername)
...     for subfolder in subfolders:
...             print('sub folder : ' + subfolder)
...     for filename in filenames:
...             print('file name : ' + filename)
...
folder name : .
sub folder : Workspace
sub folder : Workspace_Backup
file name : .DS_Store
file name : .localized
file name : Text.txt
folder name : ./Workspace
file name : .DS_Store
file name : C.txt
file name : B.txt
folder name : ./Workspace_Backup
sub folder : Test
file name : .DS_Store
file name : B.txt
file name : A.txt
folder name : ./Workspace_Backup/Test
file name : AA.txt
file name : A.txt

 

3. send2trash ๋ชจ๋“ˆ - ํœด์ง€ํ†ต

>>> import send2trash

#send2trash.send2trash() : ํŒŒ์ผ ํœด์ง€ํ†ต์œผ๋กœ ์‚ญ์ œ
>>> send2trash.send2trash('./A.txt')

 

3. zipfile ๋ชจ๋“ˆ 

 

>>> import zipfile

#zipfile.ZipFile : ์••์ถ• ํŒŒ์ผ ์ง€์ •
>>> exampleZip = zipfile.ZipFile('Image.zip')

#exampleZip.namelist() : ์••์ถ• ํŒŒ์ผ์˜ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
>>> exampleZip.namelist()
['Image/', 'Image/A.jpeg', '__MACOSX/Image/._A.jpeg', 'Image/C.jpeg', '__MACOSX/Image/._C.jpeg', 'Image/B.jpeg', '__MACOSX/Image/._B.jpeg']

#exampleZip.getinfo() : ์••์ถ• ํŒŒ์ผ์˜ ํŠน์ • ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ ์ •๋ณด ์ง€์ •
>>> aInfo = exampleZip.getinfo('Image/A.jpeg')

#file_size : ์••์ถ• ์ „ ์‚ฌ์ด์ฆˆ
>>> aInfo.file_size
1531920

#compress_size : ์••์ถ• ํ›„ ์‚ฌ์ด์ฆˆ
>>> aInfo.compress_size
1520520

mac OS ํŠน์„ฑ์ƒ zip ํ•ด์ œํ•  ๊ฒฝ์šฐ __MACOSX ํŒŒ์ผ ์ƒ์„ฑ(๋ฌด์‹œํ•ด๋„ ๋จ)

#exampleZip.extract() : ์ผ๋ถ€ ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ๋งŒ ์••์ถ• ํ•ด์ œ
>>> exampleZip.extract('Image/A.jpeg')
>>> exampleZip.close()

#exampleZip.extractall() : ์ „์ฒด ์••์ถ• ํ•ด์ œ
>>> exampleZip.extractall()
>>> exampleZip.close()

๋™์ผํ•œ ํŒŒ์ผ๋ช…์ด ์žˆ์–ด ํŒŒ์ผ๋ช…2๋กœ ์••์ถ• ํ•ด์ œ

#zipfile.ZipFile() : ์••์ถ• ํŒŒ์ผ ์ƒ์„ฑ
>>> newZip = zipfile.ZipFile('new.zip', 'w')

#zipfile.write() : ์••์ถ• ๋Œ€์ƒ ์ถ”๊ฐ€
>>> newZip.write('Text.txt', compress_type=zipfile.ZIP_DEFLATED)
>>> newZip.close()