升级后重命名脚本不起作用。需要一个新的

升级后重命名脚本不起作用。需要一个新的

递归文件重命名器 - 非常有用的 python 脚本在升级 Python(Pyt2 到 Pyt3)后停止工作。有人能找到并解决问题吗?谢谢你!

例如,这些文件:

'Linux příručka českého uživatele.pdf'
'LINUXTERO příkazy které se vždy hodí.pdf'
“Práce s archívy pří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

程序员是捷克人,所以我必须通过谷歌翻译成英语......

当我启动脚本时,按摩失败,如下所示:

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)

答案2

Python 2 现在是一种死亡语言(2020 年 1 月 1 日日落)

很长一段时间以来,人们编写的代码都与 python 2 和 3 兼容。所以它们很相似,但仍然存在差异。

也许最著名的是错误中提到的一个。 “print”语句现在需要括号:

打印“富”

就是现在

打印(“富”)

相关内容