Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a new file in Python

Status
Not open for further replies.

Wulf

Programmer
Nov 30, 2000
9
AT
I'm new to python and I've got a problem. In a python progamm I want to create a new file to write text into it. How can I create a new File in python? I've searched in the documentation but I found nothing. I've tried it with open and fopen but it didn't work. Can someone help me please?
 
[tab]Look at the function open(filename, mode). Mode can be "r" for reading, "w" for writing, and "b" for binary mode. If mode is not specified, then r is assumed. In this case, if the file does not exist, you will get an exception. To create a new file do open("myfile", w) or open("myfileb", wb) for binary mode.

[tab]To catch a file exception do the following:

Code:
try:
   file = open("/etc/password")
except IOError, (errno, message):
   if errno == 2:
      print "File does not exist, cannot open"
   else:
      print "Unhandled error", errno, message
[\code]

[tab]Some places to visit are [URL unfurl="true"]www.python.org[/URL] , [URL unfurl="true"]www.pythonlabs.com[/URL] , and [URL unfurl="true"]www.oreilly.com.[/URL] The latter has a place called O'Reilly Network. This has a lot of information about a lot of open source programs. There is a tutorial about Python by Jason Tackaberry there. The above code came from his tutorial.


 

James P. Cottingham
 
[URL unfurl="true"]www.ivcusa.com[/URL]
All opinions are mine alone and do not necessarily reflect those of my employer.
 
Thanks. But now I have another problem. I've got a variable called "table1" in my program and i want to write the information of this variable to this new created file. This variable's type is a dictionary. I tried to write the information of "table1" into the file like this:

f1 = open(file, 'w') #like you told me
f1.write(table1) #My try to write the information of "tabel1" to the file

This error occured:
Traceback (innermost last):
File "program.py", line 67, in ?
f1.write(table1)
TypeError: read-only character buffer, dictionary

How can I write the information of "table1" into the?
 
Without looking at my notes, I think you are going to have to do something like:
Code:
for x in table1
    f1.write(table1[x])

or something along those lines.


James P. Cottingham

All opinions are mine alone and do not necessarily reflect those of my employer.
 
I tried this:

for x in table1:
f1.write(table1[x])

but this doesn't work. I get this error:

Traceback (innermost last):
File "program.py", line 67, in ?
for x in table1:
TypeError: loop over non-sequence

Do you have another idea or do you know what in this loop deosn't work?
 
This message was from me. I have forgotten to log in when I wrote this:
This don't works (Visitor)

I tried this:

for x in table1:
f1.write(table1[x])

but this doesn't work. I get this error:

Traceback (innermost last):
File "program.py", line 67, in ?
for x in table1:
TypeError: loop over non-sequence

Do you have another idea or do you know what in this loop deosn't work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top