Hello Everyone, I guess This Post should be interesting as now we are in a position to go out of the Python Shell and touch the files of a host operating system.
In this post I will discuss about Handling File System. This is a Vast Topic where the inbuilt Python Module is not enough to get all our jobs done, but for starting point, and showing you how to use them, the inbuilt modules of Python works fine. The Word Module should not be unfamiliar with you in this post, and here onwards. And From here onwards I also assume that you know about Lists, tuples, Dictionary and all the stuffs that I posted Earlier.
Lets first start with how to create a file. So I am Going To show how to create a text file under the root directory of a Windows OS, i.e the C drive.
For a text file we should know, how to create, append, read and close a text file. We will work in the Python Shell, its same when you write a script in any text editor. Python interpreter would process and handle the scripts in same way.
The Basic Syntax for creating, appending, and reading is: variable_object_name = open('Relative Path', 'mode')
1. To create a text file named example.txt
>>> myFile = open('C:/example.txt', 'w') <--w means we want to write or create a text file
>>> myFile.write('This is the First Line\n')
>>> myFile.write('This is the second Line\n')
>>> myFile.writelines(''' This is blah blah blah......
....
...
....
This is the End of the Text file.\n''')
>>> myFile.close()
So first we have created a file in writing mode, and have written some lines, and finally closed the file.
write --> To write Lines in a text file line by line or as a paragraph
writelines --> To write a bunch of lines or paragraphs or to write a List we use writelines.
>>> sample = ['Python', 'Learning', 'is', 'Fun']
>>> myFile = open('C:/example.txt', 'a') <-- a means we want to append to the file
>>> myFile.writelines(sample)
>>> myFile.close()
>>>
2. To read the example.txt file
>>> myFile = open('C:/example.txt', 'r') <-- r means we want to read the file
>>> #By default, if we do not type the mode, python assumes it to be in read mode.
>>> myFile.read()
'This is the First Line\nThis is the second Line\n This is blah blah blah......\n....\n...\n....\nThis is the End of the Text file.\nPythonLearningisFun'
>>> myFile.seek(0)
>>> #seek(position) takes the python cursor to where-ever we want, since I will show some more read Functions, i have taken the cursor to the starting position.
>>> myFile.readline()
'This is the First Line\n'
>>> myFile.readline()
'This is the second Line\n'
>>> myFile.readline()
' This is blah blah blah......\n'
>>> myFile.readline()
'....\n'
>>> myFile.readline()
'...\n'
>>> myFile.readline()
'....\n'
>>> myFile.readline()
'This is the End of the Text file.\n'
>>> myFile.readline()
'PythonLearningisFun'
>>> myFile.readline()
''
>>> #readline --> reads line by line
>>> >>> myFile.seek(0)
>>> myFile.readlines()
['This is the First Line\n', 'This is the second Line\n', ' This is blah blah blah......\n', '....\n', '...\n', '....\n', 'This is the End of the Text file.\n', 'PythonLearningisFun']
>>> #readlines almost similar to read the difference is, it returns a List
>>> myFile.read()
'This is the First Line\nThis is the second Line\n This is blah blah blah......\n....\n...\n....\nThis is the End of the Text file.\nPythonLearningisFun'
>>> myFile.readlines()
[]
>>> myFile.seek(0)
>>> myFile.readlines()
['This is the First Line\n', 'This is the second Line\n', ' This is blah blah blah......\n', '....\n', '...\n', '....\n',
'This is the End of the Text file.\n', 'PythonLearningisFun']
>>> myFile.close()
>>>
To get a display in a readable manner, we use the For Iterator,
>>> myFile = open('C:/example.txt', 'r')
>>> for lines in myFile.readlines():
print lines
This is the First Line
This is the second Line
This is blah blah blah......
....
...
....
This is the End of the Text file.
PythonLearningisFun
>>> myFile.close()
Note: It is important to close a file, when you open it for any reason.
Now If You look in the C: Drive, you would see an example.txt file created.
Hope this was interesting, Practice it, play around with the functions, and Don't forget to check my next post.
Thank You.
In this post I will discuss about Handling File System. This is a Vast Topic where the inbuilt Python Module is not enough to get all our jobs done, but for starting point, and showing you how to use them, the inbuilt modules of Python works fine. The Word Module should not be unfamiliar with you in this post, and here onwards. And From here onwards I also assume that you know about Lists, tuples, Dictionary and all the stuffs that I posted Earlier.
Lets first start with how to create a file. So I am Going To show how to create a text file under the root directory of a Windows OS, i.e the C drive.
For a text file we should know, how to create, append, read and close a text file. We will work in the Python Shell, its same when you write a script in any text editor. Python interpreter would process and handle the scripts in same way.
The Basic Syntax for creating, appending, and reading is: variable_object_name = open('Relative Path', 'mode')
1. To create a text file named example.txt
>>> myFile = open('C:/example.txt', 'w') <--w means we want to write or create a text file
>>> myFile.write('This is the First Line\n')
>>> myFile.write('This is the second Line\n')
>>> myFile.writelines(''' This is blah blah blah......
....
...
....
This is the End of the Text file.\n''')
>>> myFile.close()
So first we have created a file in writing mode, and have written some lines, and finally closed the file.
write --> To write Lines in a text file line by line or as a paragraph
writelines --> To write a bunch of lines or paragraphs or to write a List we use writelines.
>>> sample = ['Python', 'Learning', 'is', 'Fun']
>>> myFile = open('C:/example.txt', 'a') <-- a means we want to append to the file
>>> myFile.writelines(sample)
>>> myFile.close()
>>>
2. To read the example.txt file
>>> myFile = open('C:/example.txt', 'r') <-- r means we want to read the file
>>> #By default, if we do not type the mode, python assumes it to be in read mode.
>>> myFile.read()
'This is the First Line\nThis is the second Line\n This is blah blah blah......\n....\n...\n....\nThis is the End of the Text file.\nPythonLearningisFun'
>>> myFile.seek(0)
>>> #seek(position) takes the python cursor to where-ever we want, since I will show some more read Functions, i have taken the cursor to the starting position.
>>> myFile.readline()
'This is the First Line\n'
>>> myFile.readline()
'This is the second Line\n'
>>> myFile.readline()
' This is blah blah blah......\n'
>>> myFile.readline()
'....\n'
>>> myFile.readline()
'...\n'
>>> myFile.readline()
'....\n'
>>> myFile.readline()
'This is the End of the Text file.\n'
>>> myFile.readline()
'PythonLearningisFun'
>>> myFile.readline()
''
>>> #readline --> reads line by line
>>> >>> myFile.seek(0)
>>> myFile.readlines()
['This is the First Line\n', 'This is the second Line\n', ' This is blah blah blah......\n', '....\n', '...\n', '....\n', 'This is the End of the Text file.\n', 'PythonLearningisFun']
>>> #readlines almost similar to read the difference is, it returns a List
>>> myFile.read()
'This is the First Line\nThis is the second Line\n This is blah blah blah......\n....\n...\n....\nThis is the End of the Text file.\nPythonLearningisFun'
>>> myFile.readlines()
[]
>>> myFile.seek(0)
>>> myFile.readlines()
['This is the First Line\n', 'This is the second Line\n', ' This is blah blah blah......\n', '....\n', '...\n', '....\n',
'This is the End of the Text file.\n', 'PythonLearningisFun']
>>> myFile.close()
>>>
To get a display in a readable manner, we use the For Iterator,
>>> myFile = open('C:/example.txt', 'r')
>>> for lines in myFile.readlines():
print lines
This is the First Line
This is the second Line
This is blah blah blah......
....
...
....
This is the End of the Text file.
PythonLearningisFun
>>> myFile.close()
Note: It is important to close a file, when you open it for any reason.
Now If You look in the C: Drive, you would see an example.txt file created.
Hope this was interesting, Practice it, play around with the functions, and Don't forget to check my next post.
Thank You.
for line in myfile:
ReplyDeleteprint line
Is a little more Pythonic - and faster, since you won't be reading all your lines into memory at once.
Also... a custom mouse cursor?
Yes Wayne, Its mentioned here, http://pythonwayz.blogspot.com/2012/04/file-handling-in-python.html,.. :)
DeleteFor Iterator is more Pythonic and yields faster result. :)
I dint understnd about the custom mouse error question??