I have multiple files whose first column contain has identifier "COM1-11","COM1-12","COM1-13","COM1-14","COM1-15"and "COM1-16" - the file name do not have identifier -
The first column of a file is like this:
"Device name: ABC-DE(COM1-11) Device type:ABC...Date:2020-07-14 10:46:59 "
Then, I want to take "COM1-11" part as identifier and move the file into corresponding folder "11","12","13","14","15" and "16" folder.
What I have reached so far:
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"
then,
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]
if Folder_identifier=="COM1-11":
shutil.move(os.path.join(Raw_data_folder,f),Folder_11)
elif Folder_identifier=="COM1-12":
shutil.move(os.path.join(Raw_data_folder,f),Folder_12)
elif Folder_identifier=="COM1-13":
shutil.move(os.path.join(Raw_data_folder,f),Folder_13)
elif Folder_identifier=="COM1-14":
shutil.move(os.path.join(Raw_data_folder,f),Folder_14)
elif Folder_identifier=="COM1-15":
shutil.move(os.path.join(Raw_data_folder,f),Folder_15)
else:
shutil.move(os.path.join(Raw_data_folder,f),Folder_16)
When I run it, it only moves only the first file into the corresponding folder - the first file has "COM-11" and it gets moved to "11" folder. Then, leave the following error message:
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'
There must be missing for loop for the whole files, but I haven't get through yet.
Try closing the file before the if-else statements in the for loop: doc.close()
.
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)
# ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.