1

I have a text file like this

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/Wallpaper/newlife.mp4', '/home/share/Wallpaper/alone.mp4', '/home/share/Wallpaper/withoutyou.mp4', '/home/share/Wallpaper/lifeline1.mp4', '/home/share/Wallpaper/movingon.mp4']


Only 1 line are included in this file!

I am trying to read moviefiles.txt and make it as a list object but I got this weird error

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

    ^
SyntaxError: unexpected EOF while parsing

Here is the error part of my code

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

NOTE:movfiles.txt is auto genterate by this file

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()

Any one knows how to fix this error?

glibdud
  • 7,550
  • 4
  • 27
  • 37

2 Answers2

3

You're doing two reads from a file which means the second read is going to be empty.

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

You should use

vdlist=movfiles.read()
print vdlist

instead.

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

Read advances the 'cursorwithin the file and without any argumentsread` tries to read as much as possible and a second read would continue where the first read ended but you're already at the end of the file after the first read. You of course can do multiple reads like this:

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

In which case the first read only advances the 'cursor' by one byte so the second read still returns some data.

You can also change the position of the cursor by using seek which means you can go back to the beginning of the file and read it again:

>>> f = open("hi.txt")
>>> f.read()
'hi\n'
>>> f.seek(0)
>>> f.read()
'hi\n'
mroman
  • 1,354
  • 9
  • 14
1
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

Guys maybe I miss understand the question, my code is working but convert each line in a list element, if you have more element in the same line it will not work. If this is the case please let me know and I'll provide an alternative solution

Carlo 1585
  • 1,455
  • 1
  • 22
  • 40