2013-12-11 143 views
0

这是我第三天使用Python,我相信某些简单的东西被忽略了。变量文件名不被视为文件,无法打开

我想索引到html文件名列表中,将索引的html文件名设置为var,然后尝试打开该文件。计划是循环遍历文件名列表。

不幸的是,这个var不是作为一个文件读取的,而是作为一个名字被读取的。

我认为这将是一个简单的问题来回答,但我只是没有找到它。

那么,我做错了什么?任何帮助将不胜感激。

这里是我的代码:

file_list = [] 
    for root, dirs, files in os.walk(r'C:\Aptana\Beautiful'): 
    for file in files: 
     if file.endswith('.html'): 
      file_list.append(file) 
input_file = file_list[0] 
orig_file = open(input_file, 'w') 

我知道我失去了一些东西简单,但我把它的驾驶我疯了!

更新:

file_list = [] 
for root, dirs, files in os.walk(r'C:\Aptana\Beautiful'): 
for file in files: 
    if file.endswith('.html'): 
     file_list.append(os.path.join(root,file)) 
    input_file = file_list[0] 
    orig_file = open(input_file, 'w') 
    soup = BeautifulSoup(orig_file) 
    title = soup.find('title')  
    main_txt = soup.findAll(id='main')[0] 
    toc_txt = soup.findAll(class_ ='toc-indentation')[0] 

然后崩溃:

Traceback (most recent call last): 
    File "C:\Aptana\beautiful\B-1.py", line 47, in <module> 
    soup = BeautifulSoup(orig_file) 
File "C:\Python33\lib\site-packages\bs4\__init__.py", line 161, in __init__ 
    markup = markup.read() 
io.UnsupportedOperation: not readable 

感谢adsmith!如果您有任何其他问题,请告诉我。

orig_file被打印为: < _io.TextIOWrapper名= 'C:\集锦\美丽模式=' R '\管理+ Guide.html' 编码= 'CP1252'>

+1

该代码看起来是正确的,一目了然。 “不作为文件读取,而是作为名称阅读”是什么意思?程序的行为是什么,你期望它做什么? –

回答

1

我看来像(使用os.stat或os.path中提供该文件的实际路径。)您当前的工作目录不在您前往的目录中。尽量不要做这样的:

file_list = [] 
    for root, dirs, files in os.walk(r'C:\Aptana\Beautiful'): 
    for file in files: 
     if file.endswith('.html'): 
      file_list.append(os.path.join(root,file)) 
input_file = file_list[0] 
orig_file = open(input_file, 'w') 

我也强烈建议您使用“与” contextlib而不是使用orig_file = open(file)orig_file.close()。相反,实施如下:

#walk through your directory as you're doing already 
input_file = file_list[0] #you know this is only for the first file, right? 
with open(input_file,'w') as orig_file: 
    #do stuff to the file 
#once you're out of the block, the file automagically closes, which catches 
#all kinds of accidental breaks in cases of error or exception. 

看起来你的问题是,你正在与“写”的标志,而不是“读”标志打开文件。我实际上并不知道BeautifulSoup是干什么的,但是快速谷歌使它看起来像一个屏幕分析器。将orig_file打开为'r'而不是'w'。

orig_file = open(input_file,'r') #your way 
#or the better way ;) 
with open(input_file,'r') as orig_file: 
    #do stuff to it in the block 

这是更好无论如何,因为打开一个文件作为“W”的空白文件:

+0

首先,感谢adsmith! – veblen

+0

我尝试了你的代码,并且所有代码似乎都可以工作,直到下一段代码中我使用美丽的汤并且它打破了。这是返回的代码:<_io。TextIOWrapper name ='C:\\ Aptana \\ Beautiful \\ Administration + Guide.html'mode ='w'encoding ='cp1252'> Traceback(最近调用最后一次): 文件“C:\ Aptana \ beautiful \ B_1.py“第47行 soup = BeautifulSoup(orig_file) 文件”C:\ Python33 \ lib \ site-packages \ bs4 \ __ init__.py“,第161行,在__init__中 markup =标记。阅读() io.UnsupportedOperation:不可读任何想法? – veblen

+0

向我展示代码,我们将找出为什么:)。听起来你可能试图使用file_list作为文件名列表和文件路径列表。请用它现在失败的代码编辑你的问题。 –