将所有文件从目录树移动到另一个目录。如果名称重复则重命名
Move all files from a directory tree to another directory. Rename if repeated names
我需要获取给定目录树中的所有文件(名为 Temp 的文件夹和包含更多子目录和文件的子目录...)加密它们并将所有内容移动到唯一目录(名为 Temp2 的文件夹,没有子目录)。如果有重复的名称,我想将名称更改为 text.txt --> text(1).txt 并继续移动重命名的文件。
这是我目前拥有的:
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)
它加密得很好,然后继续移动加密文件。问题是当它找到并尝试移动具有现有名称的文件时,它给我一个错误:
shutil.Error: Destination path
'E:\AAA\Folder\Folder\Temp2\Text.txt.aes' already exists
所以我想知道如何在移动文件的过程中重命名文件,然后移动它们,但不知道如何进行。
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
然后
os.rename(src_file_path, make_unique_filename(dest_file_path))
shutil.move 移动到目录目标。
使用起来更容易os.rename。
它将文件移动到新的目标文件。新的目标目录文件应该是唯一的,您可以使用 make_unique_filename.
这段代码现在对我有用。您的 os.path.join 还有一个问题。没有必要。 glob.glob 已经 returns 完整路径。
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)
我需要获取给定目录树中的所有文件(名为 Temp 的文件夹和包含更多子目录和文件的子目录...)加密它们并将所有内容移动到唯一目录(名为 Temp2 的文件夹,没有子目录)。如果有重复的名称,我想将名称更改为 text.txt --> text(1).txt 并继续移动重命名的文件。
这是我目前拥有的:
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)
它加密得很好,然后继续移动加密文件。问题是当它找到并尝试移动具有现有名称的文件时,它给我一个错误:
shutil.Error: Destination path 'E:\AAA\Folder\Folder\Temp2\Text.txt.aes' already exists
所以我想知道如何在移动文件的过程中重命名文件,然后移动它们,但不知道如何进行。
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
然后
os.rename(src_file_path, make_unique_filename(dest_file_path))
shutil.move 移动到目录目标。
使用起来更容易os.rename。 它将文件移动到新的目标文件。新的目标目录文件应该是唯一的,您可以使用 make_unique_filename.
这段代码现在对我有用。您的 os.path.join 还有一个问题。没有必要。 glob.glob 已经 returns 完整路径。
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)