python 將文件名寫入 txt文件

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 10 18:31:11 2020

@author: xxxd
"""
import os
import sys

path="G:\\BaiduNetdiskDownload\\最新月份(抖音熱曲)";
with open("musicName3.txt",'a',encoding='utf-8') as filetext:
    for root,dirs,files in os.walk(path):
        for name in files:
            print(os.path.join(root,name));
            filetext.write(os.path.join(root,name)+"\n");
        for name in dirs:
            print(os.path.join(root,name))
            filetext.write(os.path.join(root,name)+"\n");
    

filetext.close();


在這裏插入圖片描述

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 10 18:31:11 2020

@author: xxxd
"""
import os
import sys
#刪除文件中的空格既可打印所有文件名
path="G:\\BaiduNetdiskDownload";
#path="G:\\BaiduNetdiskDownload\\不改音響提升10倍音質";
#path="G:\\BaiduNetdiskDownload\\熱歌";
#path="G:\\BaiduNetdiskDownload\\周杰倫音樂";
#path="G:\\BaiduNetdiskDownload\\最新月份(抖音熱曲)";

MusicNUM=0;
with open("FileName20.txt",'a',encoding='utf-8') as filetext:
    for root,dirs,files in os.walk(path):
        for name in files:
            print(os.path.join(root,name));
            filetext.write(os.path.join(root,name)+"\n");
        for name in dirs:
            print(os.path.join(root,name))
            filetext.write(os.path.join(root,name)+"\n");
    

filetext.close();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章