-1
我想通過指定目錄名稱來提取文件。但我提取的目錄沒有出於某種原因提取的文件。輸出顯示zipfile已成功創建,但提取未成功完成。文件extractall不能在Python中工作
import os, shutil, send2trash, zipfile
with open('C:\\Users\\aryan\\os.walk_2.txt', 'a') as f:
for folder, subfolder, file in os.walk(r'C:\Users\Public\Downloads\coding'):
print('*' * 60,file=f)
print('THE NAME OF THE FOLDER IS {0}'.format(folder), file=f)
print('*' * 60,file=f)
for subfolders in subfolder:
print('SUBFOLDER {0} CONTAINS FOLLOWING FOLDERS {1}'.format(folder, subfolders), file=f)
for filenames in file:
print('FOLDER {0} CONTAINS FOLLOWING FILES {1}'.format(folder, filenames), file=f)
if folder == r'C:\Users\Public\Downloads\coding\Python':
if filenames == 'anti_vowel.py':
zip =zipfile.ZipFile(r'C:\Users\aryan\python_files.zip','w')
zip.write(r'C:\Users\Public\Downloads\coding\Python\anti_vowel.py', compress_type=zipfile.ZIP_DEFLATED)
print (zip.namelist())
zip.close()
print('LOOKS LIKE ZIP FILE IS CREATED')
extract_dir='C:\\Users\\aryan'
os.chdir('C:\\Users\\aryan')
if os.path.exists(extract_dir):
zip=zipfile.ZipFile('python_files.zip')
zip.extractall(extract_dir)
print(os.getcwd())
print(os.listdir())
for folder, sub, filename in os.walk(r'C:\Users\aryan'):
for file in filename:
if file == 'python_files.zip':
print ('ZIP FILE STILL PRESENT')
#os.remove('python_files.zip')
if file == 'anti_vowel.py':
print('EXTRACTED FILE IS PRESENT')
else:
print('EXTRACTED FILE IS MISSING')
zip.close()
else:
print ('THE DIR TO WHICH FILE NEEDS TO BE EXTRACTED IS MISSING')
OUTPUT: -
['Users/Public/Downloads/coding/Python/anti_vowel.py']
LOOKS LIKE ZIP FILE IS CREATED
C:\Users\aryan
['.idlerc', '.PyCharm2017.1', 'AppData', 'Application Data', 'Contacts',
'Cookies', 'Desktop', 'Documents', 'Downloads', 'Favorites', 'LEH2016',
'Links', 'Local Settings', 'Music', 'My Documents', 'NetHood',
'NTUSER.DAT', 'ntuser.dat.LOG1', 'ntuser.dat.LOG2', 'NTUSER.DAT{ac08763f-
3218-11e7-8556-93c7ff812bb9}.TM.blf', 'NTUSER.DAT{ac08763f-3218-11e7-8556-
93c7ff812bb9}.TMContainer00000000000000000001.regtrans-ms',
'NTUSER.DAT{ac08763f-3218-11e7-8556-
93c7ff812bb9}.TMContainer00000000000000000002.regtrans-ms', 'ntuser.ini',
'OneDrive', 'os.walk_2.txt', 'os.walk_result.txt', 'Pictures', 'PrintHood',
'PycharmProjects', 'python_files.zip', 'Recent', 'Saved Games', 'Searches',
'SendTo', 'Start Menu', 'Templates', 'Users', 'Videos']
ZIP FILE STILL PRESENT
EXTRACTED FILE IS MISSING
是的,你是對的。合併這些更改後,我仍然看不到目標目錄中的提取文件。 如果文件== 'python_files.zip': 打印( 'ZIP文件仍然存在') 如果文件== 'anti_vowel.py': 打印( '提取的文件存在') 打印('萃取後: - ','\ n',os.listdir('C:\\ Users \\ aryan')) – Ary
解壓後的文件應該出現在'C:\\ Users \\ aryan'中,但它不存在。 – Ary