python——獲取文件列表


 1 """--------------------------------------------------------
 2 <<獲取文件列表>>
 3 (1) os.listdir() 方法用於返回指定的文件夾包含的文件或文件夾的名字的列表。
 4 這個列表以字母順序。 它不包括 '.''..' 即使它在文件夾中。只支持在
 5 Unix, Windows 下使用。
 6 (2) os.path.join(path1[, path2[, ...]])    把目錄和文件名合成一個路徑
 7 ---------------------------------------------------------"""
 8 import os
 9 data_dir = "datacn/dialog/"
10 
11 """獲取文件列表"""
12 def getRawFileList(path):
13     """-------------------------
14     files,names=getRawFileList(raw_data_dir)
15     files: ['datacn/dialog/one.txt', 'datacn/dialog/two.txt']
16     names: ['one.txt', 'two.txt']
17     ----------------------------"""
18     files = []
19     names = []
20     for f in os.listdir(path):
21         if not f.endswith("~") or not f == "":      # 返回指定的文件夾包含的文件或文件夾的名字的列表
22             files.append(os.path.join(path, f))     # 把目錄和文件名合成一個路徑
23             names.append(f)
24     return files, names
25 
26 files,names = getRawFileList(data_dir)
27 print("files:", files)
28 print("names:", names)
E:\MySoft\Anaconda3\envs\tensorFlow\python.exe E:/MySoft/pyCharm/RNN_ChattingRobot_Day/test1.py
files: ['datacn/dialog/one.txt', 'datacn/dialog/two.txt']
names: ['one.txt', 'two.txt']

Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM