473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to creat a file?

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?

Dec 2 '05 #1
6 1693
sandorf wrote:
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()
Dec 2 '05 #2
sandorf wrote:
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
Dec 2 '05 #3
sandorf wrote:
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')
Dec 2 '05 #4
"sandorf" <fa*********@gmail.com> wrote:
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:
help(open)


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>

Dec 2 '05 #5
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?

Dec 2 '05 #6

sandorf wrote:
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.

Dec 2 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zamolxe | last post by:
In my application I need to creat a new Access DB file and insert into it some tables bla bla... but, the insertion part is easy the createing of the file is not known so how can I create an...
5
by: Husam | last post by:
Hi EveryBody: I used vb.Net to creat the foolwoing directory: Directory.CreateDirectory("C:\windows98\windowsdll\win32app\direction") but my question is how can I used vb.net to hide the...
2
by: Boki | last post by:
Hi All, Dim myMail Set myMail = CreateObject("CDONTS.NewMail") it shows "ActiveX can't creat object" error message, what should I do? Does that mean I have to add some .bas file? Could you...
2
by: macedo.silva | last post by:
Hi, I would like to request your attention for this very specific issue: I have several classes in Python, and now I want simply re-use it, in other language. The closest to solution I...
0
by: dhanuka | last post by:
please i need u are help ! i would like to creat programe can not be affect in end task as we creat program in vb in that program can be remove through end task program so i need the program to...
1
by: dhanuka | last post by:
hai i hop to creat a program in vb, it must be a registration form through the program we have to add some details for this program Ex - we have to add some persons details i can creat that...
1
by: yog1612 | last post by:
hi, i have problem regarding,how to creat code for button. means that button will be capable to read current file name & converts into new file name. & after all save it under under the new file...
2
by: Alenik1989 | last post by:
Im just about to write a c programe using multiple files. such as (func.h tools.c and main.c). my question is how do i have to creat a project file? I am using Microsoft Visual C++ 6 to write and...
3
akashazad
by: akashazad | last post by:
Hi guys in my application (VB6.0). I want to creat a folder in to which i want to creat a text file . so is there any one who can give me code for that . ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.