By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

How to create new files?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.