用于排序和重命名文件的 Python 脚本 - 删除重复项

发布于 2024-12-02 05:15:12 字数 451 浏览 4 评论 0原文

我编写了一个小脚本,需要在该脚本所在的同一文件夹中对文件进行重命名和排序。它根据文件的最后修改将文件重命名为整数(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))

这是我想出的最好的方法...问题是当存在重复项时(例如,一个已命名为 1 的文件,可能来自以前的排序)它只是将其删除。.我怎样才能防止这种情况发生?我可以对代码进行任何修改或更好的替代方法吗???

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:

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 (e.g. a file already named 1, maybe from a previous sort) it just removes it.. How can I prevent this from happening?? Is there any modification I can do to the code or a better alternative way???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短暂陪伴 2024-12-09 05:15:12

因此,这里有一个示例,它将复制到子目录并避免复制脚本的 .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)

So here's an example that will copy to a subdirectory and avoid copying your script's .pyc file as well.

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)
迷爱 2024-12-09 05:15:12

您无法一个接一个地重命名文件,因为在此过程中您可能会覆盖已排序的文件。但是,您可以先使用临时名称,然后在第二遍中将文件重命名为其最终名称:(

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))

未经测试)

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)

玻璃人 2024-12-09 05:15:12
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))
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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文