Python 2.7 Python,使用自动重命名选项复制目录

Python 2.7 Python,使用自动重命名选项复制目录,python-2.7,Python 2.7,我是新来的,请耐心点:) 我搜索了主题,但没有找到我想要的 使用Python 2.7 我想在操作文件夹内容之前复制文件夹,但每次运行代码时都必须以不同的方式重命名副本。 (原始文件夹内容大约每天都会更改,我不希望每次尝试原始文件夹中的新内容时都更改代码,也不希望丢失转换的文件) 我当前的代码: # renaming files in folder by removing any numbers from the name # the files are pictures where each p

我是新来的,请耐心点:)

我搜索了主题,但没有找到我想要的

使用Python 2.7

我想在操作文件夹内容之前复制文件夹,但每次运行代码时都必须以不同的方式重命名副本。

(原始文件夹内容大约每天都会更改,我不希望每次尝试原始文件夹中的新内容时都更改代码,也不希望丢失转换的文件)

我当前的代码:

# renaming files in folder by removing any numbers from the name
# the files are pictures where each picture is a letter a,b,c..
# by removing the numbers, the files will be sorted differently
# which will show a hidden message


import os
import shutil

def magicRename():
    # save the current path
    curPath = os.getcwd()
    # make a copy of the folder before processing the files
    shutil.copytree(r"myFolder", r"myFolder_converted")

    filesList = os.listdir(r"myFolder_converted")
    os.chdir(r"myFolder_converted")
    for fileName in filesList:
        print "The file: [" + fileName + "] renamed to: [" + fileName.translate(None, "0123456789") + "]"
        os.rename(fileName, fileName.translate(None, "0123456789"))
        print "check your files now!"
    # back to old path
    os.chdir(curPath)

# call the function
magicRename()

我想要一种自动重命名的方法,比如:文件夹\u 1,文件夹\u 2…

一种方法是计算文件夹的数量:

largest_index = max([0] + [int(f.split('_')[-1]) for f in os.listdir(curPath ) if os.path.isdir(f) and "converted" in f])
new_index = largest_index + 1
new_name = generic_name + '_%d' % new_index
基本上,这会查找格式为“myFolder\u converted\u SOMEINTEGER”的文件,找到最大的整数,向其中添加1,这就是新名称

import os
import shutil

def magicRename(folder_to_duplicate):
    # save the current path
    curPath = os.getcwd()
    # make a copy of the folder before processing the files
    all_dirs_in_path = [d for d in os.listdir(curPath ) if os.path.isdir(d)]

    all_converted_dirs_in_path = [d for d in all_dirs_in_path if 'converted' in d]

    largest_index = max([0] + [int(d.split('_')[-1]) for d in all_converted_dirs_in_path])
    new_index = largest_index + 1
    new_name = folder_to_duplicate + '_converted_%d' % new_index
    shutil.copytree(os.path.join(curPath, folder_to_duplicate), os.path.join(curPath, new_name))

    filesList = os.listdir(os.path.join(curPath, new_name))
    os.chdir(os.path.join(curPath, new_name))
    for fileName in filesList:
        print "The file: [" + fileName + "] renamed to: [" + fileName.translate(None, "0123456789") + "]"
        os.rename(fileName, fileName.translate(None, "0123456789"))
        print "check your files now!"
    # back to old path
    os.chdir(curPath)
如果您的文件类似于:

/directory
    other_directory/
/directory
运行
magicRename('other_directory')
两次,您将获得:

/directory
    other_directory/
    other_directory_converted_0/
    other_directory_converted_1/
注意:我还没有测试这段代码,这只是实现您想要的功能的一种方法,可能有一些输入错误,您应该能够自己修复它们



如果只计算文件夹的数量,您也可以做同样的事情。乍一看,这似乎比找到最大值并添加1更简单、更直观。但是,如果您决定删除其中一个文件夹,则该算法可能尝试创建一个具有现有名称的新文件夹,从而引发错误。但是,找到最大值可以确保不会发生这种情况

我不确定我是否理解这个问题。您只是想知道如何生成唯一的文件夹名称吗?从长远来看,熟悉
git
subversion
或类似的版本控制工具可能更有意义。@glibdud我只是想复制文件夹,而不给它指定新的固定名称,每次代码运行时它都必须是新的。例如:如果文件夹名为
myfolder
第一次运行时给出了
myfolder\u converted
,但第二次运行时不应覆盖该名称,而是应该给出:
myfolder\u converted\u 2
只是一个例子,我不喜欢这个命名系统。@UlrichEckhardt是的,我知道如何使用版本控制,但这对我没什么帮助,你不觉得吗?我觉得你是在重新发明那种轮子。如果你的要求不一样,那就忽略我的建议,没关系。:)我试图实现这一点,但出现了错误:
ValueError:invalid literal for int(),以10为基数:'myfiles'
,因为在运行此操作的文件夹中,文件应仅为“myFolder\u converted\u 0,myFolder\u converted\u 1”,因此在“\u”上拆分时,最后一个参数是一个数字。但是请查看编辑后的答案谢谢你特德花时间帮我,我真的很感激。我再试了一次,这次的错误是:
ValueError:max()arg是一个空序列
我不知道我的实现方式是否正确。正如我在问题中提到的,这是为了复制文件夹而不是单个文件。无法更新您的答案,
感谢您的反馈!声誉低于15的投票将被记录,但不会改变公开显示的帖子分数。