I am not understanding the error her in code

Urvish Source
import glob
from shutil import copyfile

emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"] #Define emotion order
participants = glob.glob("source_emotion\\*") #Returns a list of all folders with participant numbers

for x in participants:
    part = "%s" %x[-4:] #store current participant number
    for sessions in glob.glob("%s\\*" %x): #Store list of sessions for current participant
        for files in glob.glob("%s\\*" %sessions):
            current_session = files[20:-30]
            file = open(files, 'r')

            emotion = int(float(file.readline())) #emotions are encoded as a float, readline as float, then convert to integer.

            sourcefile_emotion = glob.glob("source_images\\%s\\%s\\*" %(part, current_session))[-1] #get path for last image in sequence, which contains the emotion
            sourcefile_neutral = glob.glob("source_images\\%s\\%s\\*" %(part, current_session))[0] #do same for neutral image

            dest_neut = "sorted_set\\neutral\\%s" %sourcefile_neutral[25:] #Generate path to put neutral image
            for i in range(len(emotion)):
                dest_emot = "sorted_set\\%s\\%s" %(emotions[emotion],
                                               sourcefile_emotion[25:]) #Do same for emotion containing image

            copyfile(sourcefile_neutral, dest_neut) #Copy file
            copyfile(sourcefile_emotion, dest_emot) #Copy file

The above code throws an error as below:

runfile('K:/Facedata landmarks/data/load_images.py', wdir='K:/Facedata landmarks/data')
Traceback (most recent call last):

  File "<ipython-input-1-0b76baf7ba54>", line 1, in <module>
    runfile('K:/Facedata landmarks/data/load_images.py', wdir='K:/Facedata landmarks/data')

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "K:/Facedata landmarks/data/load_images.py", line 21, in <module>
    emotion = int(float(file.readline())) #emotions are encoded as a float, readline as float, then convert to integer.

ValueError: could not convert string to float: '   9.0000000e+00   4.0000000e+00\n'

I am reading the file data which is string but I am reading it as float and lastly converting it to int. But then also python is throwing error.Can anyone please help me solving this? I do not understand what is going wrong here and how it can be solved.

python-3.xfilepython-multithreading

Answers

comments powered by Disqus