需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文件的绝对路径 :param file_dir: :return: ''' L = [] for root, dirs, files in os.walk(file_dir): # 获取所有文件 for file in files: # 遍历所有文件名 if os.path.splitext(file
#coding:utf8 if __name__ == '__main__': srcfile = u"D:/测试路径/测试文件.txt" f = open(srcfile.encode('gbk')) for text in f.readlines(): print text.decode('gbk') f.close()
from random import Random def random_str(randomlength=31): str = '' chars = 'abcdefghijklmnopqrstuvwxyz0123456789' length = len(chars) - 1 random = Random() for i in range(randomlength): str+=chars[random.randint(0, length)] return str x = random_str
import os filepaths = []; dirpaths = []; pathName = r'C:\anfei\json\20191128' for root, dirs, files in os.walk(pathName): for file in files: file_path = os.path.join(root, file) filepaths.append(file_path) for dir in dirs: dir_path = os.path.join(roo
在Python环境下获取指定后缀文件列表的方式. 来源stackoverflow 这里简单以*.txt的作为例子. 使用glob(推荐) import glob, os os.chdir("/mydir") for file in glob.glob("*.txt"): print(file) 简单实用os.listdir import os for file in os.listdir("./mydir"): if file.endswith(