(python)从目录结构中递归删除大写?

发布于 2024-09-06 06:03:38 字数 143 浏览 2 评论 0原文

大写字母 - 它们有什么意义?他们给你的只是RSI。

我想从我的目录结构中删除尽可能多的大写字母。我将如何编写一个脚本来在 python 中执行此操作?

它应该递归地解析指定的目录,识别大写字母的文件/文件夹名称,并将它们重命名为小写字母。

uppercase letters - what's the point of them? all they give you is rsi.

i'd like to remove as much capitalisation as possible from my directory structure. how would i write a script to do this in python?

it should recursively parse a specified directory, identify the file/folder names with capital letters and rename them in lowercase.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-09-13 06:03:38

os.walk 非常适合对文件系统进行递归操作。

import os


def lowercase_rename(folder):
    # renames all subforders of folder, not including folder itself
    def rename_all(root, items):
        for name in items:
            try:
                os.rename(os.path.join(root, name), os.path.join(root, name.lower()))
            except OSError:
                pass  # can't rename it, so what

    # starts from the bottom so paths further up remain valid after renaming
    for root, dirs, files in os.walk(folder, topdown=False):
        rename_all(root, dirs)
        rename_all(root, files)

向上遍历树的要点是,当您有像“/A/B”这样的目录结构时,您在递归期间也会有路径“/A”。现在,如果从顶部开始,您将首先将 /A 重命名为 /a,从而使 /A/B 路径无效。另一方面,当您从底部开始并首先将 /A/B 重命名为 /A/b 时,它不会影响任何其他路径。

实际上,您也可以使用 os.walk 进行自上而下的操作,但这(稍微)更复杂。

os.walk is great for doing recursive stuff with the filesystem.

import os


def lowercase_rename(folder):
    # renames all subforders of folder, not including folder itself
    def rename_all(root, items):
        for name in items:
            try:
                os.rename(os.path.join(root, name), os.path.join(root, name.lower()))
            except OSError:
                pass  # can't rename it, so what

    # starts from the bottom so paths further up remain valid after renaming
    for root, dirs, files in os.walk(folder, topdown=False):
        rename_all(root, dirs)
        rename_all(root, files)

The point of walking the tree upwards is that when you have a directory structure like '/A/B' you will have path '/A' during the recursion too. Now, if you start from the top, you'd rename /A to /a first, thus invalidating the /A/B path. On the other hand, when you start from the bottom and rename /A/B to /A/b first, it doesn't affect any other paths.

Actually you could use os.walk for top-down too, but that's (slightly) more complicated.

注定孤独终老 2024-09-13 06:03:38

尝试以下脚本:

#!/usr/bin/python

'''
renames files or folders, changing all uppercase characters to lowercase.
directories will be parsed recursively.

usage: ./changecase.py file|directory
'''

import sys, os

def rename_recursive(srcpath):
    srcpath = os.path.normpath(srcpath)
    if os.path.isdir(srcpath):
        # lower the case of this directory
        newpath = name_to_lowercase(srcpath)
        # recurse to the contents
        for entry in os.listdir(newpath): #FIXME newpath
            nextpath = os.path.join(newpath, entry)
            rename_recursive(nextpath)
    elif os.path.isfile(srcpath): # base case
        name_to_lowercase(srcpath)
    else: # error
        print "bad arg: " + srcpath
        sys.exit()

def name_to_lowercase(srcpath):
    srcdir, srcname = os.path.split(srcpath)
    newname = srcname.lower()
    if newname == srcname:
        return srcpath
    newpath = os.path.join(srcdir, newname)
    print "rename " + srcpath + " to " + newpath
    os.rename(srcpath, newpath)
    return newpath

arg = sys.argv[1]
arg = os.path.expanduser(arg)
rename_recursive(arg)

try the following script:

#!/usr/bin/python

'''
renames files or folders, changing all uppercase characters to lowercase.
directories will be parsed recursively.

usage: ./changecase.py file|directory
'''

import sys, os

def rename_recursive(srcpath):
    srcpath = os.path.normpath(srcpath)
    if os.path.isdir(srcpath):
        # lower the case of this directory
        newpath = name_to_lowercase(srcpath)
        # recurse to the contents
        for entry in os.listdir(newpath): #FIXME newpath
            nextpath = os.path.join(newpath, entry)
            rename_recursive(nextpath)
    elif os.path.isfile(srcpath): # base case
        name_to_lowercase(srcpath)
    else: # error
        print "bad arg: " + srcpath
        sys.exit()

def name_to_lowercase(srcpath):
    srcdir, srcname = os.path.split(srcpath)
    newname = srcname.lower()
    if newname == srcname:
        return srcpath
    newpath = os.path.join(srcdir, newname)
    print "rename " + srcpath + " to " + newpath
    os.rename(srcpath, newpath)
    return newpath

arg = sys.argv[1]
arg = os.path.expanduser(arg)
rename_recursive(arg)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文