How to create new files?

R

Robert Dailey

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.
 
L

Larry Bates

Robert said:
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
 
L

Lee Harr

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.: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.
 
E

Evan Klitzke

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).
 
H

Hrvoje Niksic

Robert Dailey said:
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).
 
B

Bruno Desthuilliers

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
 
A

ahlongxp

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
(e-mail address removed)
http://www.herofit.cn
 
R

Robert Dailey

Robert Dailey a écrit :





class Filestream(object):


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.







It does. But you're not using 'w', but 'rw'.


Which is what happens when trying to open an inexistant file in read mode.


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.
 
B

Bruno Desthuilliers

Robert Dailey a écrit :
On Jul 13, 3:04 am, Bruno Desthuilliers <bruno.
(e-mail address removed)> wrote: (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.
 
G

Gabriel Genellina

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.
 
S

Steve Holden

Gabriel said:
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"

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 -------------
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top