我只想把一个文件从一个文件夹移到另一个文件夹(已经知道如何做),然后在这个过程中检查目标文件夹中的所有文件并删除同名文件。在
我有两个文件夹/src和/dst。在
在/src文件夹中,我有:
- 'access.log.1.txt'
在文件夹/dst中:
- 'access.log.1.20171110_115840565311.txt'
- 'access.log.1.20171110_115940565311.txt'
- 'access.log.2.20171110_115940565311.txt'
当我将/src中的文件移到/dst时,我想删除/src中所有名为该文件的文件,但不包括/dst文件中的datetime()扩展名。在
因此/dst文件夹在执行后应该如下所示:
- 'access.log.1.txt'
- 'access.log.2.20171110_115940565311.txt'
这是我将文件从/src移到/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)
有人能帮我吗?在
谢谢!!在
Sandip的答案应该有用,但如果你特别想用你在问题中所说的方式来回答,这可能会奏效:
注意,这种方法假设所有的目标文件名都与问题中的一样,也就是说,它们都有4个句点(
.
),并且日期时间字符串总是在第三个和第四个句点之间。在删除具有匹配正则表达式的所有文件。 只需导航到目标文件夹并用正则表达式删除所有文件 在您的案例中使用
它将删除名为的所有文件访问.log.1.文本 现在,您可以使用
^{pr2}$相关问题 更多 >
编程相关推荐