简体   繁体   English

Python脚本对文件进行排序和重命名-删除重复项

[英]Python Script to sort and rename files - Removes duplicates

I've written a small script that I needed to rename and sort files in the same folder where the script is. 我编写了一个小脚本,需要在脚本所在的同一文件夹中对文件进行重命名和排序。 It renames the files into integers (1, 2, 3, 4, ...) based on the last modification of files: 根据文件的最后修改,它将文件重命名为整数(1、2、3、4,...):

import os
import sys
def gtime(nam):
    return os.path.getmtime('./'+nam)
files = os.listdir('.')
files.remove(str(sys.argv[0])[2:])
files = sorted(files, key=gtime)
for fi in range(len(files)):
    os.rename('./'+files[fi], './'+str(fi+1))

That was the best I've come up with to do so... The problem is when there's a duplicate (eg a file already named 1, maybe from a previous sort) it just removes it.. How can I prevent this from happening?? 那是我所能做到的最好的...问题是当有重复的文件(例如,一个已经命名为1的文件,可能是以前的文件)时,它只会将其删除。.如何防止这种情况发生?? Is there any modification I can do to the code or a better alternative way??? 我可以对代码进行任何修改还是更好的替代方法???

So here's an example that will copy to a subdirectory and avoid copying your script's .pyc file as well. 因此,这里有一个示例,该示例将复制到子目录,同时也避免复制脚本的.pyc文件。

import os, sys
from os.path import exists, isfile, getmtime, join as pjoin
from shutil import copyfile

targetdir='process'
stub='inputfile'

if not exists(targetdir):
  os.mkdir(targetdir)

files = [ x for x in os.listdir('.') if isfile(pjoin('.',x)) and not x.startswith(sys.argv[0]) ]
pad = len(files)/10 + 1
for i,f in enumerate(sorted(files,key=lambda x: getmtime(pjoin('.',x)))):
  copytarget = pjoin('.',targetdir,"%s-%0.*d" % (stub,pad,i))
  print "Copying %s to %s" % (f,copytarget)
  copyfile(f,copytarget)

You can't rename one file after the other, as you might overwrite already sorted files during the process. 您不能重命名一个文件,因为在此过程中您可能会覆盖已排序的文件。 You can however use temporary names first and then rename the files to their final names in a second pass: 但是,您可以先使用临时名称,然后在第二遍操作中将文件重命名为其最终名称:

import os
import sys
def gtime(nam):
    return os.path.getmtime('./'+nam)
files = os.listdir('.')
files.remove(str(sys.argv[0])[2:])
files = sorted(files, key=gtime)
for fi, file in enumerate(files):
    os.rename(file, str(fi+1)+".tmp")
for fi in range(len(files)):
    os.rename(str(fi+1)+".tmp", str(fi+1))

(untested) (未试)

import os.path
for fi in range(len(files)):
    if os.path.exists(str(fi+1)):
        print("Prevent that from happening") # whatever you want to do here
    else:
        os.rename(files[fi], str(fi+1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM