Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 变量文件名未被视为文件,无法打开_Python_File Io - Fatal编程技术网

Python 变量文件名未被视为文件,无法打开

Python 变量文件名未被视为文件,无法打开,python,file-io,Python,File Io,这是我使用Python的第三天,我确信一些简单的东西被忽略了 我试图索引到html文件名列表中,将索引的html文件名设置为var,然后尝试打开该文件。计划是循环浏览文件名列表 不幸的是,var不是作为文件读取的,而是作为名称读取的 我原以为这是一个很容易回答的问题,但我就是找不到答案 那么,我做错了什么?我们将非常感谢您的帮助 这是我的密码: file_list = [] for root, dirs, files in os.walk(r'C:\Aptana\Beautiful'):

这是我使用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
回溯(最近一次呼叫最后一次):
文件“C:\Aptana\beautifuly\B-1.py”,第47行,在
soup=BeautifulSoup(原始文件)
文件“C:\Python33\lib\site packages\bs4\\uuuuu init\uuuu.py”,第161行,在\uuuu init中__
markup=markup.read()
io.UnsupportedOperation:不可读
谢谢阿德史密斯!如果你还有其他问题,请告诉我

原始文件正在打印为:

我相信在这里可以找到一个类似的问题:


答案可能包含您正在查找的信息(使用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')
另外,我强烈建议使用“with”contextlib,而不是使用
orig\u file=open(file)
orig\u 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是做什么的,但是快速的google会让它看起来像一个屏幕解析器。以“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”打开文件会清空文件:)

这段代码乍一看似乎是正确的。您所说的“不是作为文件读取而是作为名称读取”是什么意思?程序的行为是什么?您希望它做什么?首先,感谢adsmith!我尝试了你的代码,所有的代码似乎都能工作,直到下一段代码,我用了漂亮的汤,它就坏了。下面是返回的代码:Traceback(最近一次调用last):文件“C:\Aptana\beautiful\B-1.py”,第47行,在soup=BeautifulSoup(源文件)文件“C:\Python33\lib\site packages\bs4\u init\uu.py”,第161行,在init markup=markup.read()io.unsupportdoOperation:不可读任何想法?给我看一下代码,我们会找出原因:)。听起来您可能正在尝试使用file_list作为文件名列表和文件路径列表。请使用问题当前失败的代码编辑问题。您打开的是要写入的文件,而不是要读取的文件。如果需要读取而不是写入文件,请尝试
open(input_file,'r')
。我尝试了'r',这导致[0]出现索引问题,并且没有从soup返回任何内容。谢谢杂食者!我没有看到,我做了一些搜索。今后我会努力做得更好。