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
#exampleZip.extract() : ์ผ๋ถ ํ์ผ/๋๋ ํ ๋ฆฌ๋ง ์์ถ ํด์
>>> exampleZip.extract('Image/A.jpeg')
>>> exampleZip.close()
#exampleZip.extractall() : ์ ์ฒด ์์ถ ํด์
>>> exampleZip.extractall()
>>> exampleZip.close()
#zipfile.ZipFile() : ์์ถ ํ์ผ ์์ฑ
>>> newZip = zipfile.ZipFile('new.zip', 'w')
#zipfile.write() : ์์ถ ๋์ ์ถ๊ฐ
>>> newZip.write('Text.txt', compress_type=zipfile.ZIP_DEFLATED)
>>> newZip.close()
'Programming > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ (0) | 2021.08.09 |
---|---|
selenium ํตํด์ web crawling ํด์ slack ๋ฉ์ธ์ง ๋ณด๋ด๊ธฐ (0) | 2021.06.22 |
ํ์ผ ์ด๊ณ (open) ์ฝ๊ณ (read) ์ฐ๊ธฐ(wirte) (0) | 2021.06.17 |
OS ๋ชจ๋ (0) | 2021.06.16 |
์น ํฌ๋กค๋ง(BeautifulSoup) (0) | 2021.05.06 |