Ошибка получения разрешения при переименовании Python - PullRequest
0 голосов
/ 13 февраля 2019

Я хочу переименовать файлы и подкаталоги с помощью моего скрипта Python.
просто заменив "."с пробелами.

образец дерева каталогов:

. \ fsdf.trsd.nf.g
. \ beautifyer.py
. \ lsWithSdir.py
. \ fsdf.trsd.nf.g \ fe.gre.asd
. \ fsdf.trsd.nf.g \ fa.tr.bdtxt
. \ 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 Traceback (последний последний вызов):
Файл ". \ 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'

1 Ответ

0 голосов
/ 13 февраля 2019

Вы отправили неверный путь к файлу в функцию 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)
...