简体   繁体   中英

Why a file cannot be read several times using the same handler?

I open several text file (STL) and run several operation on them using two functions previously defined. Precisely, the function "point_stl" extract the coordinates of the points contained in an STL file while the function "point_cloud" extracts the points from the STL files without repetitions.

with open(folder+"bone_set1.stl", "r") as f1, open(folder+"bone_set2.stl", "r") as f2:
    var1 = point_stl(f1,f2)
    var2 = point_cloud(f1,f2)

Why does it seem I can't use twice the variables f1 and f2? If I use them in the first function I don't get any results in the second and vice-versa.

Probably you are reading the files to the en inside the point_stl call. The remedy is to to seek the files back to position 0 before caling point_cloud :

with open(folder+"bone_set1.stl", "r") as f1, open(folder+"bone_set2.stl", "r") as f2:
    var1 = point_stl(f1,f2)
    f1.seek(0)
    f2.seek(0)
    var2 = point_cloud(f1,f2)

To understand better: the most common use case of textfiles in Python is to read line after line, operating on the data on each line - that is probably what yoru code does inside those functions. The matter is that a file, once open, holds an internal "pointer" to the position up to it were read, and from which place it will resume reading on the next call. Your first function is likely reading the files to the end, and the pointers are at the end of the file. On calling the second function, tehre is nothing left to be read.

Now, the operating system file access has this "seek" function which allows one to place the file pointer at an arbitrary position - for text files, it mostly makes sense to position it either on the begining, at the end, or at a previously stored position (in another variable). By calling it with "0", and suppressing the second ("whence") parameter , both files are re-winded to the beginning.

instead of passing in a filepointer just use f.read() and pass in file contents

with open(folder+"bone_set1.stl", "r") as f1, open(folder+"bone_set2.stl", "r") as f2:
    contentsf1 = f1.read()
    contentsf2 = f2.read()
    var1 = point_stl(contentsf1,contentsf2)
    var2 = point_cloud(contentsf1,contentsf2)

Since you pass directly the file handlers, I assume that each function reads the files content.

Unfortunatly, after the first function has read the files, the read cursors are at the end of the files and the second function has nothing to read between the cursors and the end of the files.

My advice would be to :

  • first read the files outside the functions (before calling them)
  • then pass the files content to the function instead of the file handlers.

An other solution would be to set back the read cursors at the files start after the first function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM