How to create new files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Dailey

    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.clo se()

    def __lshift__( self, data ):
    self.m_file.wri te( data )

    def __rshift__( self, data ):
    self.m_file.rea d( 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.re ad()" 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.

  • Larry Bates

    #2
    Re: How to create new files?

    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.clo se()
    >
    def __lshift__( self, data ):
    self.m_file.wri te( data )
    >
    def __rshift__( self, data ):
    self.m_file.rea d( 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.re ad()" 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_fil e.read(4) would read 4 bytes into string object pointed to by
    data.

    sizeof() in Python is len()

    -Larry

    Comment

    • Lee Harr

      #3
      Re: How to create new files?

      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.re ad()" 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.

      Comment

      • Evan Klitzke

        #4
        Re: How to create new files?

        On 7/12/07, Larry Bates <larry.bates@we bsafe.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 <evan@yelp.co m>

        Comment

        • Bruno Desthuilliers

          #5
          Re: How to create new files?

          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(obje ct):
          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.clo se()
          >
          def __lshift__( self, data ):
          self.m_file.wri te( data )
          >
          def __rshift__( self, data ):
          self.m_file.rea d( 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

          Comment

          • Hrvoje Niksic

            #6
            Re: How to create new files?

            Robert Dailey <rcdailey@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.re ad()" 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).

            Comment

            • ahlongxp

              #7
              Re: How to create new files?

              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.clo se()
              >
              def __lshift__( self, data ):
              self.m_file.wri te( data )
              >
              def __rshift__( self, data ):
              self.m_file.rea d( 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.re ad()" 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,Northea stern University,Chin a
              ahlongxp@gmail. com




              Comment

              • Robert Dailey

                #8
                Re: How to create new files?

                On Jul 13, 3:04 am, Bruno Desthuilliers <bruno.
                42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                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(obje ct):
                >
                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.clo se()
                >
                def __lshift__( self, data ):
                self.m_file.wri te( data )
                >
                def __rshift__( self, data ):
                self.m_file.rea d( 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.

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: How to create new files?

                  Robert Dailey a écrit :
                  On Jul 13, 3:04 am, Bruno Desthuilliers <bruno.
                  42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                  (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.

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: permission denied in shutil.copyfile

                    En Fri, 13 Jul 2007 12:10:20 -0300, Ahmed, Shakir <shahmed@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\dat a' or 'c:\\mydata\\da ta\'
                    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

                    Comment

                    • Steve Holden

                      #11
                      Re: permission denied in shutil.copyfile

                      Gabriel Genellina wrote:
                      En Fri, 13 Jul 2007 12:10:20 -0300, Ahmed, Shakir <shahmed@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.copyfil e(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\dat a' or 'c:\\mydata\\da ta\'
                      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 -------------

                      Comment

                      Working...