【问题标题】:Getting Permission error when renaming python重命名python时出现权限错误
【发布时间】:2019-07-07 06:51:40
【问题描述】:

我想用我的 python 脚本重命名文件和子目录。
简单地替换“。”带空格。

示例目录树:

.\fsdf.trsd.nf.g
.\beautifier.py
.\lsWithSdir.py
.\fsdf.trsd.nf.g\fe.gre.asd
.\fsdf.trsd.nf.g\fa.tr.b.d.txt
.\fsdf.trsd.nf.g\fe.gre.asd\new.path
.\fsdf.trsd.nf.g\fe.gre.asd\New.Text.Document.txt

import os
from os.path import isdir

# prompt user for path
dirPath = input("enter path of dir where the files are\n")

# subs = input("do you want to include renaming for subdirectories? (y/n)\n")

# change to path
os.chdir(dirPath)


# replace spaces
def replace_spaces(file_name):
    new_name = file_name.replace(".", " ")
    return new_name

def checkType(filePath, file):
    if os.path.isdir(file):
        new_name = replace_spaces(file)
        os.rename(filePath, os.path.join(filePath , new_name))
    else:
        file_name, file_ext = os.path.splitext(file)
        new_name = replace_spaces(file_name)
        os.rename(filePath, os.path.join(filePath , new_name + file_ext))



def get_items(dirPath):
    for path, subdirs, files in os.walk(dirPath):
        for name in subdirs:
            file_path = os.path.join(path)
            # doStuff(file_path, name)
            print(file_path + " | " + name)
            checkType(file_path, name)
        for name in files:
            file_path = os.path.join(path)
            checkType(file_path, name)


get_items(dirPath) 

错误:

。 | fsdf.trsd.nf.g 回溯(最近一次通话最后一次):
文件“.\beautifyer.py”,第 41 行,在
get_items(dirPath)
文件“.\beautifyer.py”,第 35 行,在 get_items
checkType(file_path, name)
文件“.\beautifyer.py”,第 21 行,在 checkType
os.rename(filePath, os.path.join(filePath, new_name))
PermissionError:[WinError 32] 该进程无法访问该文件,因为它正在被另一个进程使用:“。” -> '.\fsdf trsd nf g'

【问题讨论】:

    标签: python operating-system renaming


    【解决方案1】:

    您向 checkType 函数发送了错误的文件路径,修改后的代码如下:

    import os
    from os.path import isdir
    
    # prompt user for path
    dirPath = input("enter path of dir where the files are\n")
    
    # replace spaces
    def replace_spaces(file_name):
        new_name = file_name.replace(".", " ")
        return new_name
    
    def checkType(filePath, file):
        if os.path.isdir(filePath):
            new_name = replace_spaces(file)
            os.rename(filePath, os.path.join(os.path.dirname(filePath) , new_name))
        else:
            file_name, file_ext = os.path.splitext(file)
            new_name = replace_spaces(file_name)
            os.rename(filePath, os.path.join(os.path.dirname( filePath) , new_name + file_ext))
    
    
    
    def get_items(dirPath):
        for path, subdirs, files in os.walk(dirPath):
            for name in subdirs:
                file_path = os.path.join(path,name)
                # doStuff(file_path, name)
                print(file_path + " | " + name)
                checkType(file_path, name)
            for name in files:
                file_path = os.path.join(path, name)
                checkType(file_path, name)
    
    get_items(dirPath)
    

    【讨论】:

    • 这一直有效到文件夹“fe gre asd”,其中 .\fsdf.trsd.nf.g\fe.gre.asd\new.path .\fsdf.trsd.nf.g\fe.gre .asd\New.Text.Document.txt 在里面。他们仍然没有重命名
    • 如果你调试代码你会发现你只是在重命名路径的最后一部分而不是整个路径如果你想重命名整个文件夹路径你已经相应地修改了checkType函数。跨度>
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2016-12-24
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多