473,372 Members | 1,002 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,372 software developers and data experts.

How to create new files?

Hi,

I'm trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.

Basically, I want to have a "filestream" object that will allow you to
overload the '<<' and '>>' operators to stream out and stream in data,
respectively. So far this is what I have:

class filestream:
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )

# def __del__( self ):
# self.m_file.close()

def __lshift__( self, data ):
self.m_file.write( data )

def __rshift__( self, data ):
self.m_file.read( data )
So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'. I get an exception saying that the file doesn't
exist. I expected it would create the file for me. Is there a way to
make open() create the file if it doesn't exist, or perhaps there's
another function I can use to create the file? I read the python docs,
I wasn't able to find a solution.

Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.

Any help is greatly appreciated. Thanks.

Jul 12 '07 #1
10 3296
Robert Dailey wrote:
Hi,

I'm trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.

Basically, I want to have a "filestream" object that will allow you to
overload the '<<' and '>>' operators to stream out and stream in data,
respectively. So far this is what I have:

class filestream:
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )

# def __del__( self ):
# self.m_file.close()

def __lshift__( self, data ):
self.m_file.write( data )

def __rshift__( self, data ):
self.m_file.read( data )
So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'. I get an exception saying that the file doesn't
exist. I expected it would create the file for me. Is there a way to
make open() create the file if it doesn't exist, or perhaps there's
another function I can use to create the file? I read the python docs,
I wasn't able to find a solution.

Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.

Any help is greatly appreciated. Thanks.
open creates files for me, so I'm uncertain why you think it isn't for you.

the .read() method accepts the number of bytes not the buffer to store bytes
read.

data=self.m_file.read(4) would read 4 bytes into string object pointed to by
data.

sizeof() in Python is len()

-Larry
Jul 12 '07 #2
So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'. I get an exception saying that the file doesn't
exist.
Works for me...

:~$ mkdir foo
:~$ cd foo
:foo$ ls
:foo$ python
Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>f = open('bar', 'w')
f.write('test\n')
f.close()
:foo$ ls
bar
:foo$ cat bar
test
:foo$
You're going to have to show us the actual code you used and
the actual error message you are getting.
Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.
Are you trying to read in unicode?

Generally in python, you do not concern yourself with how much
space (how many bytes) a particular value takes up.

You may want to look at how the pickle module works.

Jul 12 '07 #3
On 7/12/07, Larry Bates <la*********@websafe.comwrote:
sizeof() in Python is len()
No, sizeof in C/C++ is not like len (len is more like strlen). For
example, on my computer compiling

#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv) {
char * hello = "Hello World";
printf("sizeof: %d\n", sizeof(hello));
printf("strlen: %d\n", strlen(hello));
return 0;
}

prints out 11 for strlen but only 4 for sizeof because that is the
size of a char *. AFAIK there isn't a true equivalent to sizeof in
python (I hear there is an implementation in mxtools, however).

--
Evan Klitzke <ev**@yelp.com>
Jul 12 '07 #4
Robert Dailey a écrit :
Hi,

I'm trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.

Basically, I want to have a "filestream" object that will allow you to
overload the '<<' and '>>' operators to stream out and stream in data,
respectively. So far this is what I have:

class filestream:
class Filestream(object):
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )
You don't need this C++ 'm_' prefix here - since the use of self is
mandatory, it's already quite clear that it's an attribute.

# def __del__( self ):
# self.m_file.close()

def __lshift__( self, data ):
self.m_file.write( data )

def __rshift__( self, data ):
self.m_file.read( data )
So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'.
It does. But you're not using 'w', but 'rw'.
I get an exception saying that the file doesn't
exist.
Which is what happens when trying to open an inexistant file in read mode.
I expected it would create the file for me. Is there a way to
make open() create the file if it doesn't exist
yes : open it in write mode.

def __init__( self, filename ):
try:
self._file = open( filename, "rwb" )
except IOError:
# looks like filename doesn't exist
f = open(filename, 'w')
f.close()
self._file = open( filename, "rwb" )
Or you can first test with os.path.exists:

def __init__( self, filename ):
if not os.path.exists(filename):
# looks like filename doesn't exist
f = open(filename, 'w')
f.close()
self._file = open( filename, "rwb" )

HTH
Jul 13 '07 #5
Robert Dailey <rc******@gmail.comwrites:
class filestream:
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )
[...]
So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'.
According to your code, you're not using 'w', you're using 'rwb'. In
that respect Python's open behaves the same as C's fopen.
Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.
Simply read as much data as you need. If you need to unpack external
data into Python object and vice versa, look at the struct module
(http://docs.python.org/lib/module-struct.html).
Jul 13 '07 #6
On Jul 13, 5:14 am, Robert Dailey <rcdai...@gmail.comwrote:
Hi,

I'm trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.

Basically, I want to have a "filestream" object that will allow you to
overload the '<<' and '>>' operators to stream out and stream in data,
respectively. So far this is what I have:

class filestream:
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )

# def __del__( self ):
# self.m_file.close()

def __lshift__( self, data ):
self.m_file.write( data )

def __rshift__( self, data ):
self.m_file.read( data )

So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'. I get an exception saying that the file doesn't
exist. I expected it would create the file for me. Is there a way to
make open() create the file if it doesn't exist, or perhaps there's
another function I can use to create the file? I read the python docs,
I wasn't able to find a solution.
using "w" or "wb" will create new file if it doesn't exist.
at least it works for me.
Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.
f.read(10) will read up to 10 bytes.
you know what to do now.
Any help is greatly appreciated. Thanks.
and another thing to mention, __del__() will not always be called( any
comments?).
so you'd better flush your file explicitely by yourself.

--
ahlongxp

Software College,Northeastern University,China
ah******@gmail.com
http://www.herofit.cn

Jul 13 '07 #7
On Jul 13, 3:04 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
Robert Dailey a écrit :
Hi,
I'm trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.
Basically, I want to have a "filestream" object that will allow you to
overload the '<<' and '>>' operators to stream out and stream in data,
respectively. So far this is what I have:
class filestream:

class Filestream(object):
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )

You don't need this C++ 'm_' prefix here - since the use of self is
mandatory, it's already quite clear that it's an attribute.
# def __del__( self ):
# self.m_file.close()
def __lshift__( self, data ):
self.m_file.write( data )
def __rshift__( self, data ):
self.m_file.read( data )
So far, I've found that unlike with the C++ version of fopen(), the
Python 'open()' call does not create the file for you when opened
using the mode 'w'.

It does. But you're not using 'w', but 'rw'.
I get an exception saying that the file doesn't
exist.

Which is what happens when trying to open an inexistant file in read mode.
I expected it would create the file for me. Is there a way to
make open() create the file if it doesn't exist

yes : open it in write mode.

def __init__( self, filename ):
try:
self._file = open( filename, "rwb" )
except IOError:
# looks like filename doesn't exist
f = open(filename, 'w')
f.close()
self._file = open( filename, "rwb" )

Or you can first test with os.path.exists:

def __init__( self, filename ):
if not os.path.exists(filename):
# looks like filename doesn't exist
f = open(filename, 'w')
f.close()
self._file = open( filename, "rwb" )

HTH
Thanks for the variable naming tips. Is it normal for Python
programmers to create class members with a _ prefixed?

I also figured out why it wasn't creating the file after I had posted,
I realized I was doing "rw" instead of just "w". Thank you for
verifying. Thanks to everyone else for your replies as well.

Jul 13 '07 #8
Robert Dailey a écrit :
On Jul 13, 3:04 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
(snip)
Thanks for the variable naming tips. Is it normal for Python
programmers to create class members with a _ prefixed?
This is the convention to denote implementation attributes. This won't
of course prevent anyone to access these attributes, but anyone doing so
is on it's own since it has been warned the attribute was not part of
the interface.

Jul 13 '07 #9
En Fri, 13 Jul 2007 12:10:20 -0300, Ahmed, Shakir <sh*****@sfwmd.gov>
escribió:
Is there any way to copy a file from src to dst if the dst is
exclusively open by other users.

I am using

src = 'c:\mydata\data\*.mdb'
dst = 'v:\data\all\*.mdb'

shutil.copyfile(src,dst)

but getting error message permission denied.
1) try with a local copy too, and you'll notice an error too - it's
unrelated to other users holding the file open.
2) use either r'c:\mydata\data' or 'c:\\mydata\\data\'
3) shutil.copyfile copies ONE FILE at a time.
4) use glob.glob to find the desired set of files to be copied; and
perhaps you'll find copy2 more convenient.
--
Gabriel Genellina

Jul 14 '07 #10
Gabriel Genellina wrote:
En Fri, 13 Jul 2007 12:10:20 -0300, Ahmed, Shakir <sh*****@sfwmd.gov>
escribió:
>Is there any way to copy a file from src to dst if the dst is
exclusively open by other users.

I am using

src = 'c:\mydata\data\*.mdb'
dst = 'v:\data\all\*.mdb'

shutil.copyfile(src,dst)

but getting error message permission denied.

1) try with a local copy too, and you'll notice an error too - it's
unrelated to other users holding the file open.
2) use either r'c:\mydata\data' or 'c:\\mydata\\data\'
3) shutil.copyfile copies ONE FILE at a time.
4) use glob.glob to find the desired set of files to be copied; and
perhaps you'll find copy2 more convenient.

the error is probably due to the unescaped backslash in teh destination
string - "\a"
>>dst = 'v:\data\all\*.mdb'
dst
'v:\\data\x07ll\\*.mdb'
>>>
use

dst = r'v:\data\all\*.mdb'

to avoid that problem, and learn how to use raw string literals. I am
also not sure that you can use wildcards n the destination like you can
on the Windows command-line copy command, so you may just need to give
the path to the destination directory.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Jul 14 '07 #11

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

Similar topics

17
by: Lonnie Princehouse | last post by:
In short: Is there any way to run Python WITHOUT trying to create .pyc files (or ..pyo) or to have Python not attempt to import the .pyc files it finds? Reason: We have a site-specific...
2
by: sudheervemana | last post by:
Dear all, In my main directory there are some source files and i have another directory which includes several folders,each contains the make files.Now i want to debug my source code in either...
13
by: Daniel Walzenbach | last post by:
Hi, Imagine the following situation: I have an asp.net application which allows uploading files to a SQL Server 2000 database (Files are stored as type "images"). As a next step I would like to...
9
by: Bob Achgill | last post by:
I would like to use the timestamp on files to manage the currency of support files for my VB windows application. In this case I would only put the timestamp of the file in the management database...
6
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.