Python Beginner Tutorial 7 - File IO

preview_player
Показать описание
This is an introduction to Python File IO. All Links and Slides will be in the description. Subscribe for more cool stuff!

If you like what you see be sure to subscribe and thumbs up!
Рекомендации по теме
Комментарии
Автор

I've been experimenting a bit with this exercise and I have a question for you!
Here is my Main function:

f = open('test1.txt', 'r')

for line in f:
     print line


print f.read()
f.close


Why doesn't the program print out the file twice?
Only the "for line" method actually seem to get executed.
Is "print f.read()" a part of the for loop since they are on the same vertical line? If I try to tab the for loop so that it's "behind"(as in to the right) of the "print f.read()" part i get an error message. Both ways to print out the content of the file works seemlessly if done seperately, but they don't seem to work very well together.
Also, why does the "for line" method make empty lines between the words while "print f.read()" doesn't?

Thank you for a neat tutorial!

jonatansleiers
Автор

def Main():
f = open('myfile.txt', 'r')

for line in f:
print line

f.close()

if __name__ == '__main__':
Main()

So i am using IDLE to run the my program and it says : IOError: [Errno 2] No such file or directory: 'myfile.txt
If you can help me

abstractz
Автор

In your example, is the variable f actually a pointer to that file or am I thinking too much in C? :P

nepetaleijon
Автор

hi drapTV, i find out that there are other functions like "a", "a+" and "r+".
do i need to learn them now ??? or it's not important for now ?? 
 

sbapungi
Автор

I have my main function EXACTLY like yours however whenever I run the enter one word then I enter a second word then all of the sudden the programs shuts down and I dont know why....my code is the exact same as yours yet im only allowed to enter 2 words instead of 4

seeero