2017-09-14 24 views
0

这段代码是我第一次尝试创建一个程序。运行时发生错误:如何从目录中获取每个扩展名的文件?我写了一个代码,但我得到一个例外

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\gabri\Desktop\' -> 'C:\Users\gabri\Desktop\Planilhas Excel\'

我在做什么错?这个程序的目标是获得所有的excel,然后是pdf,然后是word文件,并将它们放在程序创建的文件夹中。

import os 
from glob import glob 

# import cx_Freeze 
print("Digite o diretório de origem.") 
dirOrigem = input() 

os.chdir(dirOrigem) 

excel_files = glob('*.xlsx') 
excel_files.append(''.join(glob('*.xls'))) 

dirDestinoXL = dirOrigem + '\\' + 'Planilhas Excel' 
if not os.path.exists(dirDestinoXL): 
    os.makedirs(dirDestinoXL) 

for i in excel_files: 
    os.rename(f'{dirOrigem}\\{"".join(i)}', f'{dirDestinoXL}\\{"".join(i)}') 

os.chdir(dirOrigem) 

pdf_files = glob('*.pdf') 

dirDestinoPDF = dirOrigem + '\\' + 'PDF' 
if not os.path.exists(dirDestinoPDF): 
    os.makedirs(dirDestinoPDF) 

for p in pdf_files: 
    os.rename(f'{dirOrigem}\\{"".join(p)}', f'{dirDestinoPDF}\\{"".join(p)}') 


os.chdir(dirOrigem) 

word_files = glob('*.doc') 
word_files.append(glob('*.docx')) 

dirDestinoWord = dirOrigem + '\\' + 'Word' 
if not os.path.exists(dirDestinoWord): 
    os.makedirs(dirDestinoWord) 

for d in word_files: 
    os.rename(f'{dirOrigem}\\{"".join(d)}', f'{dirDestinoWord}\\{"".join(d)}') 
+0

如果任何这些文件在任何其他进程中打开,则关闭它们并重试。 – Antimony

+0

'excel_files = glob('*。xlsx') excel_files.append(''。join(glob('*。xls'))) 'excel_files是否包含正确的excel文件?也许是空的列表。 – deaspo

+0

删除:excel_files.append(''。join(glob('*。xls')))它工作正常。你正在追加一个空白部分,所以你的列表看起来像[''],然后你的for循环尝试修改''它不能。 –

回答

1

我想你的计划,这是行不通的,因为它是我的电脑上。我改变了一些线,它的工作原理。希望它有帮助

import os 
from glob import glob 

dirOrigem = r'C:\Users\fchal\Desktop\temp' # here I changed the code just because I didn't want to bother using input() 
os.chdir(dirOrigem) 

excel_files = glob('*.xlsx') 
excel_files.extend(glob('*.xls')) 


dirDestinoXL = dirOrigem + '\\' + 'xlsfile' 
if not os.path.exists(dirDestinoXL): 
    os.makedirs(dirDestinoXL) 

for i in excel_files: 
    os.rename(i, os.path.join(dirDestinoXL, i)) 


# same procedure for pdf and word files 
+0

这看起来很有趣。我把输入,因为我不会在我的电脑上运行,我会在她的电脑上需要正确的目录。但我想我只会得到她的PC的目录。 –

0

我知道glob有时可能是一团糟。如果文件已打开,则可能会出错。这是我会做:

import os 

def move_files_with_extension(from_dir, to_dir, *extensions): 
    if not os.path.isdir(from_dir): 
     raise ValueError('{} is not a real directory'.format(from_dir)) 
    elif not os.path.isdir(to_dir): 
     raise ValueError('{} is not a real directory'.format(to_dir)) 

    files_with_extensions = all_files_with_extensions_in(from_dir, *extensions) 

    for file_path in files_with_extensions: 
     os.rename(file_path, os.path.join(to_dir, os.path.basename(file_path))) 

def all_files_with_extensions_in(dir, *extensions): 
    files_with_extensions = list() 

    for dir_path, dir_names, file_names in os.walk(dir): 
     for file_name in file_names: 
      if file_name.endswith(extensions): 
       files_with_extensions.append(os.path.join(dir_path, file_name)) 

    return files_with_extensions 

,然后你可以这样做:

dirOrigem = input() 

excel_location = os.path.join(dirOrigem, 'Planilhas Excel') 

move_files_with_extension(dirOrigem, excel_location, '.xls', '.xlsx') 

+0

这是如此美丽。我仍然无法掌握所有这些工作。这就是所谓的好代码,我的是人们称之为坏代码的东西?没问题,这是我的第一个程序。 –

+0

哈哈!我不会称你的代码不好,我的代码也不好。对同一问题只是不同的解决方案。 –

相关问题