Python 根据条件将多个文件移动到多个目录中

Python 根据条件将多个文件移动到多个目录中,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有多个文件,其第一列包含标识符“COM1-11”、“COM1-12”、“COM1-13”、“COM1-14”、“COM1-15”和“COM1-16”-文件名没有标识符- 文件的第一列如下所示: “设备名称:ABC-DE(COM1-11)设备类型:ABC…日期:2020-07-14 10:46:59” 然后,我想将“COM1-11”部分作为标识符,并将文件移动到相应的文件夹“11”、“12”、“13”、“14”、“15”和“16”中 到目前为止,我所取得的成就: import codecs im

我有多个文件,其第一列包含标识符“COM1-11”、“COM1-12”、“COM1-13”、“COM1-14”、“COM1-15”和“COM1-16”-文件名没有标识符-

文件的第一列如下所示:

“设备名称:ABC-DE(COM1-11)设备类型:ABC…日期:2020-07-14 10:46:59”

然后,我想将“COM1-11”部分作为标识符,并将文件移动到相应的文件夹“11”、“12”、“13”、“14”、“15”和“16”中

到目前为止,我所取得的成就:

import codecs
import pandas as pd
import shutil
import os

Raw_data_folder = r'C:\Users\Downloads\move_file_test'
files=os.listdir(Raw_data_folder)

Folder_11=r"C:\Users\Downloads\wished_dest\11"
Folder_12=r"C:\Users\Downloads\wished_dest\12"
Folder_13=r"C:\Users\Downloads\wished_dest\13"
Folder_14=r"C:\Users\Downloads\wished_dest\14"
Folder_15=r"C:\Users\Downloads\wished_dest\15"
Folder_16=r"C:\Users\Downloads\wished_dest\16"
那么

当我运行它时,它只会将第一个文件移动到相应的文件夹中-第一个文件有“COM-11”,它会被移动到“11”文件夹中。 然后,留下以下错误消息:

PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process:
'C:\\Users\\Downloads\\move_file_test\\Data_20200714_104659741.csv'

整个文件必须缺少for循环,但我还没有完成。

尝试在for循环中的if-else语句之前关闭文件:
doc.close()


文件是否在其他应用程序(如Excel)中打开?否,我关闭了所有csv文件。奇怪的是,错误消息中提到的文件--“Data_20200714_104659741.csv”已经被移动了。但是,它不会移动到下一个文件。在这种情况下,可以尝试在for循环中的if-else语句之前关闭该文件:
doc.close()
。您好,zmike。它工作完美!!我感谢你的帮助:)
PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process:
'C:\\Users\\Downloads\\move_file_test\\Data_20200714_104659741.csv'
for f in files:
    doc=codecs.open(os.path.join(Raw_data_folder,f),'rU','UTF-16')
    identifier_1 = pd.read_csv(doc, sep='\t',nrows=0)
    identifier_2 = identifier_1 .columns.str[21:28]
    Folder_identifier=identifier_2 [0]
    
    # Close the file
    doc.close()

    if Folder_identifier=="COM1-11":
        shutil.move(os.path.join(Raw_data_folder,f),Folder_11)
    # ...