第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對于目錄,將子目錄中的文件重命名為子目錄名+舊文件名到新目錄

對于目錄,將子目錄中的文件重命名為子目錄名+舊文件名到新目錄

料青山看我應如是 2022-01-11 16:12:49
我有一個包含多個子文件夾的文件夾,每個子文件夾里面都有一個文件。我正在使用 python 并希望使用關聯(lián)的子文件夾名稱加上舊文件名重命名文件以成為新文件名。我已經(jīng)能夠獲得子文件夾和文件名的列表,os.walk()但是我在更改文件名時遇到了問題。def list_files(dir):    r = []    for root, dirs, files in os.walk(dir):        for name in files:            r.append(os.path.join(root, name))            os.rename(name, r)我得到錯誤:類型錯誤:重命名:dst 應該是字符串、字節(jié)或 os.PathLike,而不是列表當我返回 r 時,我得到了根目錄和文件名,但無法更改文件名。感謝任何幫助。
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經(jīng)驗 獲得超5個贊

import os

def list_files(dir):

    sub_folders = os.listdir(dir)

    for sub_folder in sub_folders:

        sub_folder_path = os.path.join(dir,sub_folder)

        for root, dirs, files in os.walk(sub_folder_path):

            for file in files:

                new_filename = root.replace(dir, "").replace(os.path.sep,"_").strip("_")+"_" + file

                os.rename(os.path.join(root, file), os.path.join(root, new_filename))

input_dir = ""

assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"

list_files(input_dir)

這將為多個子目錄而不是嵌套目錄完成這項工作。如果要更改文件名的格式,請更改new_filename = sub_folder+ "" + file.


查看完整回答
反對 回復 2022-01-11
?
互換的青春

TA貢獻1797條經(jīng)驗 獲得超6個贊

它listdir()在兩個級別都使用,它檢查并忽略第一級子目錄中更深的嵌套目錄。


這是我的代碼,注釋很好:


import os


def collapse_dirs(dir):


    # get a list of elements in the target directory

    elems = os.listdir(dir)


    # iterate over each element

    for elem in elems:


        # compute the path to the element

        path = os.path.join(dir, elem)


        # is it a directory?  If so, process it...

        if os.path.isdir(path):


            # get all of the elements in the subdirectory

            subelems = os.listdir(path)


            # process each entry in this subdirectory...

            for subelem in subelems:


                # compute the full path to the element

                filepath = os.path.join(path, subelem)


                # we only want to proceed if the element is a file.  If so...

                if os.path.isfile(filepath):


                    # compute the new path for the file - I chose to separate the names with an underscore,

                    # but this line can easily be changed to use whatever separator you want (or none)

                    newpath = os.path.join(path, elem + '_' + subelem)


                    # rename the file

                    os.rename(filepath, newpath)


def main():

    collapse_dirs('/tmp/filerename2')


main()

這是我在運行代碼之前的目標目錄:


filerename2

├── a

│   └── xxx.txt

├── b

│   ├── xxx.txt

│   └── yyyy

│       └── zzzz

├── c

│   └── xxx.txt

└── xxxx

這是之后:


filerename2

├── a

│   └── a_xxx.txt

├── b

│   ├── b_xxx.txt

│   └── yyyy

│       └── zzzz

├── c

│   └── c_xxx.txt

└── xxxx


查看完整回答
反對 回復 2022-01-11
  • 2 回答
  • 0 關注
  • 267 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號