473,779 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1710
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('Hell o 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*********@gm ail.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
1487
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 Access db file\?
5
10059
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 directory? any help will be appreciated
2
1330
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 please advice?
2
1853
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 think I came was with this site: http://www.py2exe.org/index.cgi/Py2exeAndCtypesComExeServer?highlight=%28%28Py2exeAndCtypesComDllServer%29%29
0
1167
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 creat can not be do any thing using end task funtion thank u dhanuka(basic vb user )
1
2772
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 part but i have no any idea about how that detail save and how it add for my program Ex - need to save all the detail and as we whish want to display it agin as the definition it has thes conditions 1.to save all the persons details 2.as we...
1
1353
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 name. ( Final result should be following way Report name - Warehouse code - Date) Format For Date : YYYYMMDD. XLS PLZ Try to reply immediate Thanks. yog1612@yahoo.co.in
2
1837
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 compile my codes......
3
9682
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 . thankx in advance
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.