我有一个python代码来读取在Windows上运行良好的数据文件。抱歉,如果这很简单-我是新来的Python。你知道吗
现在我试着在Linux上运行它,但它不起作用。我相应地更改了所有路径,问题似乎是在读取配置文本文件中的变量之后,当它试图列出文件名时。你知道吗
以下是代码片段和结果:
{This is called to read the configuration file at the beginning of the code. It runs and seems to work fine:}
def readConfig(fn):
# dynamically create the dictionary from the config file
# check for duplication
config = {}
file = open(fn,'r')
lines = file.readlines()
for x in lines:
# print x ,re.search("=",x)
if re.search('=',x) > 0:
# print x
parts = x.split("=")
key = parts[0]
parts = parts[1].split(" ")
parts = parts[0].split("\n")
value = parts[0]
config[key]=value #this appends, if key exists it will take
print ''
print config
print ''
file.close()
return config
{Here's where it tries to list the files in the source directory. Source directory src is defined in the configuration file:}
# 5. scan in the src directory and process those files
src = halo1Config['SRC']
files = listFiles(src,".hpl")
print src
print files
{This is what Listfiles does:}
def listFiles(src,ext):
# get list of files from directory src and subdir below with ext
# ext ="" is okay
fns=[]
for dirName, subdirList, fileList in os.walk(src, topdown=True):
# print('Found directory: %s' % dirName)
for fname in fileList:
# print('\t%s' % fname)
if fname.lower().endswith(ext):
fn = os.path.join(dirName,fname)
fns.append(fn)
return fns
在这段代码中,SRC=/home/data/,在这个文件夹中有一些名为User1\u 2015.hpl的文件-这些是数据文件(文本)。代码正确地输出src,但是它为文件输出[],即使src目录中有一长串文件。因此,问题一定是与列表文件,但我看不出有什么问题。。。当我在Windows上使用它时,它工作得很好!你知道吗
有人知道我做错了什么吗?你知道吗
谢谢!你知道吗
您可以尝试:
相关问题 更多 >
编程相关推荐