How to creat a file?

S

sandorf

I'm new to python. Have a simple question.

"open" function can only open an existing file and raise a IOerror when
the given file does not exist. How can I creat a new file then?
 
L

Laurent RAHUEL

sandorf said:
I'm new to python. Have a simple question.

"open" function can only open an existing file and raise a IOerror when
the given file does not exist. How can I creat a new file then?

fic = open('test.txt', 'w')
fic.write('Hello world')
fic.close()
 
W

Wolfram Kraus

sandorf said:
I'm new to python. Have a simple question.

"open" function can only open an existing file and raise a IOerror when
the given file does not exist. How can I creat a new file then?
open the new file in write mode: open('foo', 'w')
See: help(open)

HTH,
Wolfram
 
J

Juho Schultz

sandorf said:
I'm new to python. Have a simple question.

"open" function can only open an existing file and raise a IOerror when
the given file does not exist. How can I creat a new file then?

You already have two correct answers. A warning: if you open a existing
file for writing, it is truncated (the old contents of that file
disappear.) For adding new content to the end of an existing file,
you can use:

f = open(filename,'a')
 
F

Fredrik Lundh

sandorf said:
I'm new to python. Have a simple question.

"open" function can only open an existing file and raise a IOerror when
the given file does not exist. How can I creat a new file then?

reading the documentation might help:

class file(object)
| file(name[, mode[, buffering]]) -> file object
|
| Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
| writing or appending. The file will be created if it doesn't exist
| when opened for writing or appending; it will be truncated when
| opened for writing. Add a 'b' to the mode for binary files.
| Add a '+' to the mode to allow simultaneous reading and writing.

/snip/

| Note: open() is an alias for file().

this is also explained in the chapter 7 of the tutorial ("reading and writing files"),
as well as the reference manual.

</F>
 
S

sandorf

Thank to you all, guys. Here's another question:

I'm using the Windows version of Python and IDLE. When I debug my .py
file, my modification to the .py file does not seem to take effect
unless I restart IDLE. Saving the file and re-importing it doesn't help
either. Where's the problem?
 
M

mensanator

sandorf said:
Thank to you all, guys. Here's another question:

I'm using the Windows version of Python and IDLE. When I debug my .py
file, my modification to the .py file does not seem to take effect
unless I restart IDLE. Saving the file and re-importing it doesn't help
either. Where's the problem?
From your edit window, go to the Run menu and select Run Module.

It will prompt you to save the file, it will restart the shell and will
reload
and run the new version all in one operation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top