I’m working in a Jupyter notebook. I want to open a kml file in 1 cell and do some analysis in the next. After selecting a file , the first cell has:
# Print the selected path, filename, or both
print(fc.selected_path)
print(fc.selected_filename)
print(fc.selected)
global mytext
with open(fc.selected, 'r') as f:
print(f.read())
mytext = f.read()
This prints out the contents of the file as expected.
In the next cell , I try to print out my text , but I get an empty string.
How do I pass the contents of the file to the my text variable in the next cell?
>Solution :
f.read() exhausts a file and leaves the cursor at the end of the file, therefore mytext doesn’t receive any more content.
Try reading first and then printing:
with open(fc.selected, 'r') as f:
mytext = f.read()
print(mytext)
