[英]searching for a filename with extension and printing its relative path
我有以下代码来打印文件名,这是文件扩展名为* .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中将我视为新手
您需要使用os.path.join
:
def get_filelist() :
directory = "\\\\networkpath\\123\\abc\\"
filelist = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('org'): # here 'org' will be in small letter
print(os.path.join(root,file))
filelist.append(os.path.join(root,file))
return filelist
files
和dirs
列出了root
的子代。 因此, dirs
列出了file
兄弟姐妹。 您想打印此:
print(os.path.relpath(os.path.join(root, file)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.