我有以下代码来打印文件名,这是文件扩展名为* .org的查找条件。如何打印找到的文件的相对路径。提前致谢
def get_filelist() :
directory = "\\\\networkpath\\123\\abc\\"
filelist = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('Org'):
print(str(dirs) +"\\" + str(file)) #prints empty list [] followed by filename
filelist.append(os.path.splitext(file)[0])
return (filelist)
请在python中将我视为新手
files
并dirs
列出的孩子root
。dirs
因此列出的兄弟姐妹file
。您想打印此:
print(os.path.relpath(os.path.join(root, file)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句