Python獲取當前目錄下所有檔案的絕對路徑並儲存在檔案中

garfielder007發表於2016-05-11

Python獲取當前目錄或者其他指定目錄下所有檔案的絕對路徑並儲存在檔案中,程式如下:

import os

fid = open('GetAbsolutePath.txt', 'w')

rootdir = os.getcwd() 
# rootdir = 'D:\\test' 
# print('rootdir = ' + rootdir)

pathname = []

for (dirpath, dirnames, filenames) in os.walk(rootdir):     
    for filename in filenames:  
		pathname += [os.path.join(dirpath, filename)]

fid.write(str(pathname)
fid.close()


相關文章