简体   繁体   English

Python:使用来自列表的名称重命名文件夹中的多个文件

[英]Python: Rename multiple files in folder with name that come from a list

I want to build a small app that renameS files that are in a folder with some names that are in a txt file like so:我想构建一个小应用程序,重命名文件夹中的文件,其中一些名称位于 txt 文件中,如下所示:

in C:\python\Folder i have files: 1436790.pdf 1436791.pdf 1436792.pdfC:\python\Folder我有文件: 1436790.pdf 1436791.pdf 1436792.pdf

in file.txt I have 3 lines:file.txt我有 3 行:

  1. x X
  2. y
  3. z z

Renaming should be:重命名应该是:

  1. 1436790.pdf -> x.pdf 1436790.pdf -> x.pdf
  2. 1436791.pdf -> y.pdf 1436791.pdf -> y.pdf
  3. 1436792.pdf -> z.pdf 1436792.pdf -> z.pdf

I can't figure out how os.rename() is working.我不知道os.rename()是如何工作的。 I'm getting this error我收到这个错误

**"OSError: [WinError 87] The parameter is incorrect:"**

I tried to make this app in a similar way with Total Commander multi rename tool (load names from file).我尝试使用 Total Commander 多重重命名工具(从文件加载名称)以类似的方式制作此应用程序。

basefolder=r'C:\python\Folder'
with open('file.txt', 'r') as fh:
    read_lines = fh.readlines()
files=os.listdir(basefolder)
for file in r(files):
    fn,fext=os.path.splitext(files[file])
    os.rename(files[file], read_lines[file]+fext )

Looks like this is due to the pytest/Python bug issue.看起来这是由于 pytest/Python 错误问题。 Refer https://github.com/pytest-dev/pytest/issues/5724参考https://github.com/pytest-dev/pytest/issues/5724

For now try downgrading to Python 3.7.3 until someone finds a fix.现在尝试降级到 Python 3.7.3,直到有人找到修复程序。

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

相关问题 python 重命名文件列表,在后面添加文件夹名称 - python Rename list of files adding it's folder name behind 使用 Python 重命名文件夹中的文件,使用 Excel 中的名称映射 - Rename files in a folder using Python, using name map from Excel 使用Excel工作表中的名称映射,借助Python Script重命名文件夹中文件的名称 - Rename the name of files in a folder with the help of Python Script, using name map from Excel sheet 使用Python工作表中的名称映射,借助Python脚本重命名文件夹中的文件名 - Rename the name of files in the folder with the help of Python Script, using name map from Excel sheet 如何使用 python 中的 file.txt 中的文件名重命名文件夹中的文件? - how to rename files in a folder with the files name in file.txt in python? Python - 根据列表重命名多个文件 - Python - Rename multiple files based on list 如何重命名文件夹中遵循特定名称模式的多个文件 - How to rename multiple files in a folder that follow a specific name pattern Python 重命名文件夹中的文件与 CSV 文件中的名称匹配 - Python to rename files in a folder with matching name in CSV file 使用python列表中的名称重命名文件 - Rename files with names from a list in python Python-将Excel文件读入列表并重命名文件夹中的文件 - Python - read excel file into a list and rename files in a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM