Python 循环浏览文件
我在名为1.htm-100.htm的文件夹中有100个文件。 我运行此代码从一个文件中提取一些信息,并将提取的信息放在另一个文件final.txt中。目前,我必须手动运行100个文件的程序。我需要构造一个循环,可以运行程序100次,读取每个文件一次。(请详细解释我需要在代码中进行的精确编辑) 以下是文件6.htm的代码:Python 循环浏览文件,python,file,loops,Python,File,Loops,我在名为1.htm-100.htm的文件夹中有100个文件。 我运行此代码从一个文件中提取一些信息,并将提取的信息放在另一个文件final.txt中。目前,我必须手动运行100个文件的程序。我需要构造一个循环,可以运行程序100次,读取每个文件一次。(请详细解释我需要在代码中进行的精确编辑) 以下是文件6.htm的代码: import glob import BeautifulSoup from BeautifulSoup import BeautifulSoup fo = open("6.
import glob
import BeautifulSoup
from BeautifulSoup import BeautifulSoup
fo = open("6.htm", "r")
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
htmltext = fo.read()
soup = BeautifulSoup(htmltext)
#print len(urls)
table = soup.findAll('table')
rows = table[0].findAll('tr');
for tr in rows:
cols = tr.findAll('td')
for td in cols:
text = str(td.find(text=True)) + ';;;'
if(text!=" ;;;"):
bo.write(text);
bo.write('\n');
fo.close()
bo.close()
b= open("output.txt", "r")
for j in range (1,5):
str=b.readline();
for j in range(1, 15):
str=b.readline();
c=str.split(";;;")
#print c[1]
if(c[0]=="APD ID:"):
f.write(c[1])
f.write("#")
if(c[0]=="Name/Class:"):
f.write(c[1])
f.write("#")
if(c[0]=="Source:"):
f.write(c[1])
f.write("#")
if(c[0]=="Sequence:"):
f.write(c[1])
f.write("#")
if(c[0]=="Length:"):
f.write(c[1])
f.write("#")
if(c[0]=="Net charge:"):
f.write(c[1])
f.write("#")
if(c[0]=="Hydrophobic residue%:"):
f.write(c[1])
f.write("#")
if(c[0]=="Boman Index:"):
f.write(c[1])
f.write("#")
f.write('\n');
b.close();
f.close();
f.close();
print "End"
然后,剩下的代码就到了那里。也考虑(最佳实践) 因此,不要忘记关闭这些文件句柄,因为操作系统中允许打开的文件句柄数量有限,这将确保不会错误地填充 使您的代码看起来像这样:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
另外,从不替换全局变量名,如str
:
str=b.readline();
也不需要代码>在代码行的末尾,这是Python。。我们以舒适的方式编码
最后但并非最不重要
if(c[0]=="APD ID:"):
if(c[0]=="Name/Class:"):
if(c[0]=="Source:"):
if(c[0]=="Sequence:"):
if(c[0]=="Length:"):
if(c[0]=="Net charge:"):
if(c[0]=="Hydrophobic residue%:"):
if(c[0]=="Boman Index:"):
应该是:
if(c[0]=="APD ID:"):
elif(c[0]=="Name/Class:"):
elif(c[0]=="Source:"):
elif(c[0]=="Sequence:"):
elif(c[0]=="Length:"):
elif(c[0]=="Net charge:"):
elif(c[0]=="Hydrophobic residue%:"):
elif(c[0]=="Boman Index:"):
除非您在修改过程中修改了c
,当然,您不需要这样做。。所以,切换
妈的,我只是一直在寻找关于这段代码的更可怕的东西(很明显,你已经从所有星系的示例中粘贴了副本…):
您可以将上述所有if
/elif
/else
压缩为一个if块:
if(c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:")):
f.write(c[1])
f.write("#")
另外,再次跳过if块周围的(
…)
。。这是Python。。我们以舒适的方式进行编程:
if c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:"):
f.write(c[1])
f.write("#")
然后,剩下的代码就到了那里。
也考虑(最佳实践)
因此,不要忘记关闭这些文件句柄,因为操作系统中允许打开的文件句柄数量有限,这将确保不会错误地填充
使您的代码看起来像这样:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
另外,从不替换全局变量名,如str
:
str=b.readline();
也不需要代码>在代码行的末尾,这是Python。。我们以舒适的方式编码
最后但并非最不重要
if(c[0]=="APD ID:"):
if(c[0]=="Name/Class:"):
if(c[0]=="Source:"):
if(c[0]=="Sequence:"):
if(c[0]=="Length:"):
if(c[0]=="Net charge:"):
if(c[0]=="Hydrophobic residue%:"):
if(c[0]=="Boman Index:"):
应该是:
if(c[0]=="APD ID:"):
elif(c[0]=="Name/Class:"):
elif(c[0]=="Source:"):
elif(c[0]=="Sequence:"):
elif(c[0]=="Length:"):
elif(c[0]=="Net charge:"):
elif(c[0]=="Hydrophobic residue%:"):
elif(c[0]=="Boman Index:"):
除非您在修改过程中修改了c
,当然,您不需要这样做。。所以,切换
妈的,我只是一直在寻找关于这段代码的更可怕的东西(很明显,你已经从所有星系的示例中粘贴了副本…):
您可以将上述所有if
/elif
/else
压缩为一个if块:
if(c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:")):
f.write(c[1])
f.write("#")
另外,再次跳过if块周围的(
…)
。。这是Python。。我们以舒适的方式进行编程:
if c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:"):
f.write(c[1])
f.write("#")
然后,剩下的代码就到了那里。
也考虑(最佳实践)
因此,不要忘记关闭这些文件句柄,因为操作系统中允许打开的文件句柄数量有限,这将确保不会错误地填充
使您的代码看起来像这样:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
另外,从不替换全局变量名,如str
:
str=b.readline();
也不需要代码>在代码行的末尾,这是Python。。我们以舒适的方式编码
最后但并非最不重要
if(c[0]=="APD ID:"):
if(c[0]=="Name/Class:"):
if(c[0]=="Source:"):
if(c[0]=="Sequence:"):
if(c[0]=="Length:"):
if(c[0]=="Net charge:"):
if(c[0]=="Hydrophobic residue%:"):
if(c[0]=="Boman Index:"):
应该是:
if(c[0]=="APD ID:"):
elif(c[0]=="Name/Class:"):
elif(c[0]=="Source:"):
elif(c[0]=="Sequence:"):
elif(c[0]=="Length:"):
elif(c[0]=="Net charge:"):
elif(c[0]=="Hydrophobic residue%:"):
elif(c[0]=="Boman Index:"):
除非您在修改过程中修改了c
,当然,您不需要这样做。。所以,切换
妈的,我只是一直在寻找关于这段代码的更可怕的东西(很明显,你已经从所有星系的示例中粘贴了副本…):
您可以将上述所有if
/elif
/else
压缩为一个if块:
if(c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:")):
f.write(c[1])
f.write("#")
另外,再次跳过if块周围的(
…)
。。这是Python。。我们以舒适的方式进行编程:
if c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:"):
f.write(c[1])
f.write("#")
然后,剩下的代码就到了那里。
也考虑(最佳实践)
因此,不要忘记关闭这些文件句柄,因为操作系统中允许打开的文件句柄数量有限,这将确保不会错误地填充
使您的代码看起来像这样:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
另外,从不替换全局变量名,如str
:
str=b.readline();
也不需要代码>在代码行的末尾,这是Python。。我们以舒适的方式编码
最后但并非最不重要
if(c[0]=="APD ID:"):
if(c[0]=="Name/Class:"):
if(c[0]=="Source:"):
if(c[0]=="Sequence:"):
if(c[0]=="Length:"):
if(c[0]=="Net charge:"):
if(c[0]=="Hydrophobic residue%:"):
if(c[0]=="Boman Index:"):
应该是:
if(c[0]=="APD ID:"):
elif(c[0]=="Name/Class:"):
elif(c[0]=="Source:"):
elif(c[0]=="Sequence:"):
elif(c[0]=="Length:"):
elif(c[0]=="Net charge:"):
elif(c[0]=="Hydrophobic residue%:"):
elif(c[0]=="Boman Index:"):
除非您在修改过程中修改了c
,当然,您不需要这样做。。所以,切换
妈的,我只是一直在寻找关于这段代码的更可怕的东西(很明显,你已经从所有星系的示例中粘贴了副本…):
您可以将上述所有if
/elif
/else
压缩为一个if块:
if(c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:")):
f.write(c[1])
f.write("#")
另外,再次跳过if块周围的(
…)
。。这是Python。。我们以舒适的方式进行编程:
if c[0] in ("APD ID:", "Name/Class:", "Source:", "Sequence:", "Length:", "Net charge:", "Hydrophobic residue%:", "Boman Index:"):
f.write(c[1])
f.write("#")
可能是这样的结构:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
可能是这样的结构:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
可能是这样的结构:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
可能是这样的结构:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
列出特定目录中的所有文件
正如@Torxed所指出的,最佳实践是使用with子句(以便关闭文件句柄)
您可以在中查找.htm文件,如下所示:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
列出特定目录中的所有文件
正如@Torxed所指出的,最佳实践是使用with子句(以便关闭文件句柄)
您可以在中查找.htm文件,如下所示:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
列出特定目录中的所有文件
正如@Torxed所指出的,最佳实践是使用with子句(以便关闭文件句柄)
您可以在中查找.htm文件,如下所示:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
列出特定目录中的所有文件
正如@Torxed所指出的,最佳实践是使用with子句(以便关闭文件句柄)
您可以在中查找.htm文件,如下所示:
import os
with open("final.txt","a+") as f:
for root, folders, files in os.walk('./path/to/html_files/'):
for fileName in files:
with open(os.path.abspath(root + '/' + fileName, "r") as fo:
...
# declare main files
bo = open("output.txt" ,"w")
f = open("final.txt","a+")
#loop over range ii = [1,100]
for ii in range(1,101):
fo = open(str(ii) + ".htm", "r")
# Run program like normal
...
...
...
fo.close()
f.close()
bo.close()
import os
# Creates a list of 1-100.htm file names
filenames = map(lambda x: str(x) + ".htm", range(1,101))
for file in os.listdir("/mydir"):
if (file in filenames):
# Do your logic here.
这就是为什么。而且,对于范围(1,5)中的j,:
从未使用过吗?或者至少你没有在任何地方使用j
,而且标签缩进完全是w