獲取目錄下的檔名稱
阿新 • • 發佈:2020-12-11
需求
期望整理出電腦中所有的pdf文件列表,把自己看過的書,羅列出來,整理一下自己的財產。
程式碼
import os import os.path as op import platform fileNames=[] print(platform.system()) if(platform.system()=='Windows'): sep='\r\n' elif(platform.system()=='Linux'): sep='\n' else: sep='\n' def main(): #os.path.pa print("start") get_filename_pdf() write_file() def get_filename_pdf(): fileItems=os.listdir('.') print(os.path.abspath(".")) for item in fileItems: #print(os.path.abspath(item)) if op.isfile(item) and item.endswith(".pdf"): # print() fileNames.insert(0,(item+sep).encode()) elif op.isdir(item): os.chdir(item) get_filename_pdf() os.chdir("..") def write_file(): with open("file.txt",'wb') as file: print(fileNames) file.writelines(fileNames) if __name__=="__main__": main()
拓展
可以通過配置支援拓展。也可以做其他的功能,檔案整理。
總結
python的包用的不熟