【问题标题】:How to read a file into a list [duplicate]如何将文件读入列表[重复]
【发布时间】:2018-12-15 05:21:09
【问题描述】:

我有一个这样的文本文件

moviefiles.txt

['/home/share/Wallpaper/Hymnfortheweekend(remix).mp4', '/home/share/Wallpaper/mrkittengrove.mp4', '/home/share/Wallpaper/lovelyrabbitandstarycat.mp4', '/home/ share/Wallpaper/candygirl(tsar_remix).mp4', '/home/share/Wallpaper/ninelie.mp4', '/home/share/Wallpaper/allweknow.mp4', '/home/share/Wallpaper/Nanamori.mp4' , '/home/share/Wallpaper/Fragments.mp4', '/home/share/Wallpaper/alter.mp4', '/home/share/Wallpaper/memsofyou.mp4', '/home/share/Wallpaper/luvletter. mp4', '/home/share/Wallpaper/atthedge.mp4', '/home/share/Wallpaper/lifeline.mp4', '/home/share/Wallpaper/power.mp4', '/home/share/Wallpaper/ yiran.mp4', '/home/share/Wallpaper/iknewyouwereintroubl.mp4', '/home/share/Wallpaper/lookwhatyoumademedo.mp4', '/home/share/Wallpaper/continue.mp4', '/home/share/壁纸/newlife.mp4'、'/home/share/Wallpaper/alone.mp4'、'/home/share/Wallpaper/withoutyou.mp4'、'/home/share/Wallpaper/lifeline1.mp4'、'/home/分享/Wallpaper/movingon.mp4']


此文件中仅包含 1 行!

我正在尝试读取 moviefiles.txt 并将其作为列表对象,但我收到了这个奇怪的错误

Traceback (most recent call last):
  File "wallpaper.py", line 8, in <module>
    vdlist = eval(vdlist)
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

这是我的代码的错误部分

movfiles = open("movfiles.txt", "r")
print (movfiles.read())
vdlist=movfiles.read()
vdlist = eval(vdlist)

注意:movfiles.txt 由该文件自动生成

import glob
from tkinter.filedialog import askdirectory
folder = askdirectory()
print (folder)
mp4files=glob.glob(folder+"/*.mp4")
movfiles=glob.glob(folder+"/*.mov")
avifiles=glob.glob(folder+"/*.avi")
flvfiles=glob.glob(folder+"/*.flv")
allvideofiles=mp4files+movfiles+avifiles+flvfiles
print (mp4files)
file = open("movfiles.txt","w")
file.write(str(allvideofiles))
file.close()

有人知道如何解决这个错误吗?

【问题讨论】:

  • 出于安全原因,请始终使用ast.literal_eval 而不是eval
  • 使用eval 无论如何都不是一个好方法。最好将列表作为换行符分隔的条目写入文件,然后逐行再次读取文件。
  • 我在使用 ast 文件“”时遇到这样的错误,第 0 行 ^ SyntaxError: unexpected EOF while parsing
  • 读取文件的内容并将其作为代码评估是吗?我也喜欢危险的生活!

标签: python string python-3.x list file


【解决方案1】:

你正在从一个文件中读取两次,这意味着第二次读取将是空的。

movfiles = open("movfiles.txt", "r")
print (movfiles.read())
vdlist=movfiles.read() # this is empty.

你应该使用

vdlist=movfiles.read()
print vdlist

改为。

>>> f = open("hi.txt")
>>> f.read()
'hi\n'
>>> f.read()
''

Read 推进 'cursorwithin the file and without any argumentsread` 尝试尽可能多地读取,第二次读取将在第一次读取结束的地方继续,但在第一次读取后您已经位于文件末尾。您当然可以像这样进行多次读取:

>>> f = open("hi.txt")
>>> f.read(1)
'h'
>>> f.read()
'i\n'

在这种情况下,第一次读取只会将“光标”前移一个字节,因此第二次读取仍会返回一些数据。

您也可以使用 seek 改变光标的位置,这意味着您可以回到文件的开头并再次阅读:

>>> f = open("hi.txt")
>>> f.read()
'hi\n'
>>> f.seek(0)
>>> f.read()
'hi\n'

【讨论】:

    【解决方案2】:
    movfiles = open("movfiles.txt", "r")#open the file in reading mode
    a= (movfiles.readlines())#read all the lines and save in a list where each line is an element
    print (a)#print your list
    

    伙计们,也许我错过了理解这个问题,我的代码正在工作,但转换列表元素中的每一行,如果您在同一行中有更多元素,它将无法正常工作。 如果是这种情况,请告诉我,我会提供替代解决方案

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2016-10-03
      • 2012-03-09
      • 2023-04-07
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2011-03-17
      相关资源
      最近更新 更多