简体   繁体   English

将所有文件从目录树移动到另一个目录。 重命名,如果重复名称

[英]Move all files from a directory tree to another directory. Rename if repeated names

I need to get all the files in a given directory tree (folder named Temp and subdirectories with more subdirectories and files...) encrypt them and move everything to a unique directory (folder named Temp2 with no subdirectories).我需要获取给定目录树中的所有文件(名为 Temp 的文件夹和具有更多子目录和文件的子目录......)对它们进行加密并将所有内容移动到一个唯一的目录(名为 Temp2 的文件夹,没有子目录)。 If there are repeated names, I want to change name to, let's say, text.txt --> text(1).txt and continue moving that renamed file.如果有重复的名称,我想将名称更改为 text.txt --> text(1).txt 并继续移动该重命名的文件。

This is what I have at the moment:这就是我目前所拥有的:

bufferSize = 64 * 1024
password1 = 'password'

print('\n> Beginning recursive encryption...\n\n')
for archivo in glob.glob(sourcePath + '\\**\*', recursive=True):
   fullPath = os.path.join(sourcePath, archivo)
   fullNewf = os.path.join(destinationPath, archivo + '.aes')

   if os.path.isfile(fullPath):
      print('>>> Original: \t' + fullPath + '')
      print('>>> Encrypted: \t' + fullNewf + '\n')
      pyAesCrypt.encryptFile(fullPath, fullNewf, password1, bufferSize)
      shutil.move(fullPath + '.aes', destinationPath)

It encrypts just fine and proceeds to move encrypted files.它加密得很好,然后继续移动加密文件。 The problem is that when it finds and tries to move a file with the an existing name, it gives me an error:问题是,当它找到并尝试移动具有现有名称的文件时,它给了我一个错误:

shutil.Error: Destination path 'E:\AAA\Folder\Folder\Temp2\Text.txt.aes' already exists shutil.Error:目标路径“E:\AAA\Folder\Folder\Temp2\Text.txt.aes”已经存在

So I need to know how to rename files with repeated names in the proccess of moving them and then move them, but don't know how to proceed.所以我需要知道如何在移动它们的过程中重命名具有重复名称的文件然后移动它们,但不知道如何继续。

def make_unique_filename(file_path):
    duplicate_nr = 0
    base, extension = os.path.splitext(file_path)
    while os.path.exists(file_path):
        duplicate_nr += 1
        file_path = f'{base}({duplicate_nr}){extension}'

    return file_path

and then接着

os.rename(src_file_path, make_unique_filename(dest_file_path))

shutil.move moves to a directory destination. shutil.move 移动到目录目标。

It is easier to use os.rename.使用 os.rename 更容易。 It moves a file to a new destination file.它将文件移动到新的目标文件。 The new destination dir file should be unique, wihich you can do with make_unique_filename.新的目标 dir 文件应该是唯一的,您可以使用 make_unique_filename 来做到这一点。

This code is working for me now.这段代码现在对我有用。 There was another problem with your os.path.join.您的 os.path.join 还有另一个问题。 It is not necessary.没有必要。 glob.glob already returns a full path. glob.glob 已经返回完整路径。

import pyAesCrypt
import os
import glob

sourcePath = r'E:\test aes\src'
destinationPath = r'E:\test aes\dst'

bufferSize = 64 * 1024
password1 = 'password'

def make_unique_filename(file_path):
    duplicate_nr = 0
    base, extension = os.path.splitext(file_path)
    while os.path.exists(file_path):
        duplicate_nr += 1
        file_path = f'{base}({duplicate_nr}){extension}'

   return file_path


for archivo in glob.glob(sourcePath + '\\**\*', recursive=True):
    fullPath = archivo
    fullNewf = archivo + '.aes'

    if os.path.isfile(fullPath):
        print('>>> Original: \t' + fullPath + '')
        print('>>> Encrypted: \t' + fullNewf + '\n')
        pyAesCrypt.encryptFile(fullPath, fullNewf, password1, bufferSize)

        destination_file_path = os.path.join(destinationPath, os.path.split(fullNewf)[1])
        destination_file_path = make_unique_filename(destination_file_path)
        print(destination_file_path)
        os.rename(fullNewf, destination_file_path)

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

相关问题 重命名文件并将其复制到新目录的脚本。 - Script to rename and copy files to a new directory. 将所有文件从一个目录移动到另一个磁盘空间有限的目录 - Move all files from one directory to another with limited disk space 如何将某个目录中所有文件的一定百分比移动到另一个目录中? - How to move a a certain percentage of all the files in a directory into another directory? 使用 Paramiko 将文件从一个目录移动到另一个目录 - Move files from one directory to another with Paramiko 根据条件将文件从一个目录移动到另一个目录 - Move Files from a Directory to Another Based on a Condition 如何将文件从一个目录移动到另一个目录? - How to move files from a directory to another? 在树目录中检测到但未重命名的文件(用于重命名) - Files (for rename) detected but not renamed in tree directory 如何重命名然后将特定的子文件夹移动到另一个目录? - How to rename then move specific subfolders to another directory? Python 将目录中的所有文件移动到另一个目录中带有时间戳的子目录 - Python move all files in directory to sub-directory with timestamp in another directory Python递归移动子目录下的所有文件到另一个目录 - Recursively move all files on subdirectories to another directory in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM