Python-列出文件夹中的文件-带有变量的路径名 [英] Python - Listing files in folders - path names with a variable
本文介绍了Python-列出文件夹中的文件-带有变量的路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对此很陌生.我正在使用python,并且希望在多个不同的文件夹中列出文件(使用Windows)
I'm very very new to this. I'm using python and I want to list files in a number of different folders (using windows)
在我的第一步中,我有很多路径变量.每个路径都有其自己的变量.它奏效了,但这似乎是一个漫长的尝试.由于路径与文件夹名称相同,因此我尝试了以下方法:
In my first go I had loads of path variables. Each path had its own variable. It worked but this seemed like a long winded way of doing it. As the paths are all the same apart from the folder name, I tried this:
import os
folder = ["folderA", "folderB", "folderC", "folderD"]
path1 = input('//server/files/"%s"/data' % (folder))
def list_sp_files():
for filename in os.listdir(path1):
print path1, filename
print "reporter"
list_sp_files()
我了解为什么它不起作用,但是我不明白如何使它起作用.
I understand why it doesn't work, but I don't understand how I make it work.
推荐答案
也许是这样的事情?
folders = ["folderA", "folderB", "folderC", "folderD"]
def list_sp_files():
for folder in folders:
path = '//server/files/%s/data' % (folder)
for filename in os.listdir(path):
print path, filename
这篇关于Python-列出文件夹中的文件-带有变量的路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文