2017-05-23 13 views
-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 

答えて

0

問題はここにある:'python_files.zip'に等しい

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場合、それは'anti_vowel.py'にも等しくすることはできませんので、その値を変更せずそれは常にであるelse部分

+0

あなたは正しいです。これらの変更を組み込んだあとも、抽出先のディレクトリに抽出されたファイルは表示されません。 ファイル== 'python_files.zip' の場合: プリント( 'STILL PRESENT ZIPファイル') ファイルの場合== 'anti_vowel.py': プリント( '抽出されたファイルが存在している')抽出後 印刷( ': - '、' \ '、os.listdir(' C:\\ Users \\ aryan ')) – Ary

+0

解凍後のファイルは' C:\\ Users \\ aryan 'に表示されますが、そこにはありません。 – Ary

関連する問題