Page 1 of 1

Working with files in python

#1 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 17 January 2009 - 05:34 PM

When you close a program, all the data are disappeared and if you run it again, then it starts with a clean slate. So when you want to store your program data to manipulate them at later date, you simply need to preserve it by writing files.

First of all you need to give Python access to a file by opening it. You can do it by using open() function. The open function takes two parameters-first name of the file to be accessed, and second a mode. The available modes are-

1.’r’:Read mode(file is opened for reading)
2.’w’:Write mode(file is opened for writing and if a file with the same name is exists, then it will be erased)
3.’a’:Append mode(file is open for appending, which means data is to be added with the existing data)

Lets write our first file-

#opens file in writing mode which creates a file named “welcome.txt”
>>> f=open("welcome.txt","w")
>>> print f
<open file 'welcome.txt', mode 'w' at 0x00AFBC80>

Methods of file object
With the statement “f=open(“welcome.txt”,”w”)”, an object of the class ‘file’ is created. To read from or write to the file read, readline, or write methods are used. Finally, when works with files are finished, then close method is used to close the file.

write method to file object is used write into files. Here is how to do this-

#opens file in writing mode
		Welcome to Python.
	   Hope you enjoy it.’’’
#write content to file
#close the file

To read the contents of a file, read method is used. Take a look at the following code-

#opens the file in reading mode
#here argument size is used to read mehtod
#here no argument is supplied
#close the file

The code above will result the following output-
'Hi!\nWelcome '
'Hi!\nWelcome to Python.\nHope you enjoy it.'

Note: consists an optional argument size. If it is negative or not supplied, the entire content will be read. Or otherwise you can specify it.

We can also read the contents of a file line by line by using looping statement-

for line in f:
	print line

Which results the following output-
Welcome to Python.
Hope you enjoy it.

Another method to file object is used to read contents of a file named ‘readline’. See how it works-


The output is-
‘Welcome to Python.\n’
‘Hope you enjoy it.\n’
['Hi!\n', 'Welcome to Python.\n', 'Hope you enjoy it.']

f.readline() just read a single line. When you use f.readline() for the first time in a program, it will read first line. Next time use of f.readline() results the read of second line and so on. If you wish to read the entire file, then use f.readlines(). It is a good practice to close a file after your works are finished.

These are the basic methods to the file object. There are other methods also. You may take a look at these by entering the command help(file) to your interpreter. Hope you clearly understand this tutorial.
Happy coding!

Is This A Good Question/Topic? 1
  • +

Replies To: Working with files in python

#2 andreas_g  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-March 10

Posted 03 April 2010 - 07:00 PM

Nice tut!!! Thank you! :yes:

This post has been edited by andreas_g: 03 April 2010 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 698
  • Joined: 19-January 09

Posted 30 May 2010 - 03:39 AM

Hello! Nice tutorial.
I am having some problems with creating and using files at a certain path. I am using the pythoncard file dialog to get the user to select a file but when it returns a path it returns it as a list and i can't use the
file = open('File', 'w')
method that you would normally use. This is what I have so far so if you can help me at all that would be great
wildcard = "Note Manager files (*.enm)|*.enm|All Files (*.*)|*.*
findFile = dialog.fileDialog(self, 'Open', '', '', wildcard )
FilePath = findFile.paths
File = open(FilePath, 'w')

Thanks in advance :turned:

This post has been edited by Eric115: 30 May 2010 - 03:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 344
  • Joined: 22-November 07

Posted 02 November 2010 - 06:01 PM

Did you ever figure this out? I don't know much about dialogs but I am GUESSING that 'findFile.paths' gives you a list of filenames? If you are only ever going to be interested in the first one then this would work:
 File = open(findFile.paths[0], 'w') 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1