我试图通过指定目录名来提取文件。但是我提取的目录由于某种原因没有提取的文件。输出显示已成功创建zipfile但未成功完成提取。
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')
输出: -
['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
答案 0 :(得分:0)
问题出在这里:
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')
如果file
等于'python_files.zip'
,那么它也不能等于'anti_vowel.py'
而不更改其值,因此它始终位于else
1}}部分
答案 1 :(得分:0)
简而言之,我认为您无法读取超出容器范围的压缩文件内容。首先,我几乎没有开始软件开发指导,也不了解任何事情,但是我确实了解您要完成的工作。同样,当我加载音乐软件程序FL Studio时,音频文件处于压缩状态(以zip文件压缩),DAW看到的是文件对象。拖动时,压缩文件图标将与之交互并触发响应。设置并在悬停时产生视觉变化(例如),但是当加载状态指示器完成时,音序器中没有轨道。我认为也许正在发生的事情是IDE无法看到压缩文件内容容器以及部分或全部元数据,但是看不到您定义的实际数据文本,因此无法生成预期的文件。我知道可以从某些想法(如VS代码)和某些扩展包中读取压缩文件。另外,正在使用什么终端工具?