Since the target file was path information, only the file name is acquired in advance.
py:os.path.basename
file_name = os.path.basename(txt_fle_line)[:-1]
XCOPY I also tried using XCOPY temporarily with "subprocess.call", I also needed to move, so I decided to use shutil for versatility.
subprocess.call
import subprocess
subprocess.call("xcopy %s %s /D /K /R /Y /C /V /E"%(file_path, send))
Since we adopted the new one at the time of update, we took time and compared it.
shutil.py
if os.path.exists(rename_file) :
copy_date = os.stat(rename_file).st_mtime
org_date = os.stat(file_path).st_mtime
if copy_date < org_date:
print "%s copy... %s" %(send, grid_name)
shutil.copy2(file_path, rename_file)
else:
A script that edits the file name while copying the target file by referring to the text.
copy_target.py
import os
import shutil
from datetime import datetime as dt
txt_file = open(r"copy_target.txt","r")
for target_line in txt_file:
if target_line == '\n':
break
target_cl = target_line[:-1].split(',')
file_path = target_cl[0].strip('"')
file_name = target_cl[1].strip('"')
grid_cl = file_name.split('_')
if len(grid_cl) == 6:
photo = grid_cl[0]
area = grid_cl[1]
b_cl = grid_cl[2]
b_rw = grid_cl[3]
b_no = grid_cl[4]
flg = grid_cl[5]
grid_name = area + b_cl + b_rw
send = r'G:\grid\\' + area + '\\' + grid_name
if os.path.isdir(send) :
rename_file = send +'\\'+ file_name
if os.path.exists(rename_file) :
copy_date = os.stat(rename_file).st_mtime
org_date = os.stat(file_path).st_mtime
if copy_date < org_date:
print "%s copy... %s" %(send, grid_name)
shutil.copy2(file_path, rename_file)
else:
print "%s copy new... %s" %(send, grid_name)
shutil.copy2(file_path, rename_file)
else:
is_not_file = open(r"D:\python\file_serch\target_work\is_not_GRID.txt", "a+")
is_not_file.write(file_path + "," + file_name + "," + "no path" + "\n")
is_not_file.close()
Recommended Posts