I just want to move a file from one folder to another (already know how to do that) and in the process check all the files in the destination folder and delete the files with the same name.
I have two folders /src and /dst.
In folder /src I have:
- 'access.log.1.txt'
and in folder /dst :
- 'access.log.1.20171110_115840565311.txt'
- 'access.log.1.20171110_115940565311.txt'
- 'access.log.2.20171110_115940565311.txt'
When I move the file in /src to /dst I want to delete all the files named as the file in /src excluding the datetime() extension in the files of /dst.
So the /dst folder should look like this after the execution:
- 'access.log.1.txt'
- 'access.log.2.20171110_115940565311.txt'
This is the code I have to move files from /src to /dst:
entrada = ENTRADA #This 3 are the paths to the folders /src
salida = SALIDA # /dst
error=ERROR # /err
files=glob.glob(entrada)
for file in files:
fichero=open(file,encoding='utf-8')
try:
for line in fichero:
la=line.replace("-","")
li=la.replace('”'+chr(10),'')
li=li.split('"')
line_DB(li)
fichero.close()
if TIME_RENAME=='True':
execution=str(datetime.now())
execution=execution.replace('.','')
execution=execution.replace('-','')
execution=execution.replace(' ','_')
execution_time=execution.replace(':','')
base = os.path.splitext(file)[0]
base=base+'.'+execution_time+'.txt'
os.rename(file,base)
file=base
else:
print('Hello')
#This is where I need the code
shutil.move(file, salida)
con.commit()
except:
logging.error(sys.exc_info())
print(sys.exc_info())
fichero.close()
shutil.move(file, error)
Anyone could help me?
Thanks!!