アップグレード後、名前変更スクリプトは機能しません。新しいものが必要です。

アップグレード後、名前変更スクリプトは機能しません。新しいものが必要です。

再帰的なファイル名の変更 - Python(Pyt2からPyt3へ)をアップグレードした後、非常に便利なPythonスクリプトが機能しなくなりました。誰でも問題を見つけて解決できますか?ありがとうございます!

たとえば、次のファイルは次のようになります。

「Linux príručka českého user.pdf」
'LINUXTERO príkazy které se vždy hodí.pdf'
"Práce s Archivy príkazové řádka.pdf"
「Rekurzivní grep.txt」

次のファイルを変更します。

linux_prirucka_ceskeho_uzivatele.pdf '
linuxtero_prikazy_ktere_se_vzdy_hodi.pdf
prace_s_archivy_prikazove_radka.pdf
rekurzivni_grep.txt

プログラマーはチェコ人であるため、Googleから英語に翻訳する必要があります...

スクリプトを起動すると、マッサージは次のように失敗します。

linq@legion5:~/test-renamer/ask_stackexchange_folder$ vycisti.py
ファイル "/home/linq/bin/vycisti.py"、62行
print "警告: '%s' はまだ有効な名前ではありません。" %fileName
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^
構文エラー: 'print'を呼び出すと括弧がありません。印刷(...)を意味しますか?

スクリプトは次のとおりです。

#!/usr/bin/python
# -*- coding:  utf-8

'''Renames all files and directories in the current directory to
    to have some culture. It also works recursively in nested ones
     directories. The changes are as follows:
     Czech characters -> English
     cancels all dashes and spaces at the beginning of the name (damn, what kind
             the names make up would deserve a few slaps)
     special characters are replaced by '_', I take the hyphen at mercy, but not at the beginning
     the (unnecessary) '_' around the hyphen is discarded
     multiple '_'s are replaced by a single occurrence
     everything will be converted to lowercase

     The result should be a title where there are only letters, underscores,
     numbers, periods and dashes (but not at the beginning).
     It writes exactly what it does to standard output
     Usage: vycisti.py [ > logfile ]
'''

import re
import os
import sys
import unicodedata

class RenameFiles:
    def __init__(self):
        if os.environ.get('OS','') == 'Windows_NT':
            self.locale = "cp1250"
        else: #probably linux
            local = os.environ.get('LANG', '')
            if '.' in local: #Fedora way
                self.locale = local.split('.')[1]
            else: #Debian
                self.locale = 'iso-8859-2'

    def cleanString(self, what):
        '''Gets rid of letters which are not in English alphabet'''
        assert type(what) == unicode
        normalized = unicodedata.normalize('NFKD', what)
        output = ''
        for c in normalized:
            if not unicodedata.combining(c):
                output += c
        return output

    def cleanName(self, fileName):
        '''Convert the givne string into a form which is suitable for a file name'''
        assert type(fileName) == str
        fileName = self.cleanString(fileName.decode(self.locale))
        fileName = re.sub("^[-\ ]+", "", fileName) #delete space or dash at the beginning
        invalid_stuff = re.compile(r"[^a-zA-Z0-9_\.-]+") #forbidden characters (non-alfanumerical)
        fileName = invalid_stuff.sub("_", fileName.strip()) #replace invalid stuff and spaces by _,
        fileName = re.sub("_+", "_", fileName) #squeeze continuous underscores to one _
        fileName = re.sub("-+", "-", fileName) #squeeze continuous dashes to one _
        fileName = re.sub("_*-_*", "-", fileName) #removes useless '_' round the dash
        fileName = re.sub("_*\._*", ".", fileName) #removes useless '_' round the dot
        fileName = re.sub("-*\.-*", ".", fileName) #removes useless '-' round the dot
        fileName = fileName.lower() #lower case
        valid_name=re.compile(r"^[a-z0-9_\.][a-z0-9_\.-]+$") #regular expression for feasible name
        if not valid_name.match(fileName):
            print "Warning: '%s' is still not valid name" % fileName
        return fileName.encode(self.locale)

    def renameFile(self, dir, fileName):
        '''Public: Renames the file fileName in the directory'''
        assert type(fileName) == str
        assert type(dir) == str
        try:
            new = self.cleanName(fileName)
        except:
            print "Problem: %s %s " % (fileName, sys.exc_info()[0] )
            new = ""
        if (new != "" and new != fileName):
            print "Renaming %s: %s -> %s" % (dir, fileName, new) #kontrolní výpis
            os.rename(dir+os.sep+fileName, dir+os.sep+'tmp')
            os.rename(dir+os.sep+'tmp', dir+os.sep+new)
            return

    def process_dir(self, dir):
        """process all files in the folder"""
        assert type(dir) == str
        for f in os.listdir(dir):
            fileName = dir + os.sep + f
            if os.path.isdir(fileName) and os.access(fileName, os.W_OK): #if it is directory
                self.process_dir(fileName) #process the elements in the directory
                self.renameFile(dir, f) #rename the directory itself
            else:
                self.renameFile(dir, f) #if it is a file
        return

if __name__=='__main__':
        renamer = RenameFiles()
        renamer.process_dir('.')

ベストアンサー1

コメントで述べたように、Python2は廃止されました。この問題を解決するための別のツールがあります...

使用真珠rename:

$ rename -u utf8 -n  '
    BEGIN{use Text::Unidecode} # 'import' needed extra module
    s/.*/unidecode($&)/e;      # decode utf8 -> ascii
    s/\s+/_/g;                 # s/[[:space:]]/_/g
    y/A-Z/a-z/                 # translate uppercase to lower
' ./**/*.*                     # ** enable recursion¹

1zshデフォルトでは、bashはshopt -s globstarコマンドの前に実行する必要があります。

sudo apt install rename libtext-unidecode-perlパッケージ()が必要ですDebian*/Ubuntu/Mint...

出力がうまくいったら削除してください-n(別名)。dry-run

出力

rename(Linux příručka českého uživatele.pdf, linux_prirucka_ceskeho_uzivatele.pdf)
rename(LINUXTERO příkazy které se vždy hodí.pdf, linuxtero_prikazy_ktere_se_vzdy_hodi.pdf)
rename(Práce s archívy příkazové řádka.pdf, prace_s_archivy_prikazove_radka.pdf)
rename(Rekurzivní grep.txt, rekurzivni_grep.txt)

おすすめ記事