homedir, file copy

Discussion in 'Python' started by ecu_jon, Jan 30, 2011.

  1. ecu_jon

    ecu_jon Guest

    hello,
    i am trying to work with windows homedirectory as a starting point for
    some kind of file copy command. i'm testing this on a win7 box so my
    home is c:\Users\jon\
    here is the code snippet i am working on:

    import os

    homedir = os.path.expanduser('~')
    try:
    from win32com.shell import shellcon, shell
    homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    except ImportError:
    homedir = os.path.expanduser("~")
    print homedir
    print os.listdir(homedir+"\\backup\\")
    homedir.replace("\\\\" , "\\")
    print homedir
    shutil.copy (homedir+"\\backup\\", homedir+"\\backup2\\")


    output looks like:
    C:\Users\jon
    ['test1.txt', 'test2.txt']
    C:\Users\jon

    Traceback (most recent call last):
    File "D:\spring 11\capstone-project\date.py", line 43, in <module>
    shutil.copy (homedir+"\\backup\\", homedir+"\\backup2\\")
    File "C:\Python27\lib\shutil.py", line 116, in copy
    copyfile(src, dst)
    File "C:\Python27\lib\shutil.py", line 81, in copyfile
    with open(src, 'rb') as fsrc:
    IOError: [Errno 2] No such file or directory: 'C:\\Users\\jon\\backup\
    \'


    why is there still two \\ in the pathfor the copy command?
     
    ecu_jon, Jan 30, 2011
    #1
    1. Advertising

  2. ecu_jon

    r Guest

    On Jan 30, 2:44 pm, ecu_jon <> wrote:

    > shutil.copy (homedir+"\\backup\\", homedir+"\\backup2\\")


    TIP: Use os.path.join(x,y, z*)

    > why is there still two \\ in the pathfor the copy command?


    I always convert my paths to use a single '/' instead of '\\'. Just
    makes life that much easier!
     
    r, Jan 30, 2011
    #2
    1. Advertising

  3. ecu_jon

    ecu_jon Guest

    On Jan 30, 3:55 pm, r <> wrote:
    > On Jan 30, 2:44 pm, ecu_jon <> wrote:
    >
    > > shutil.copy (homedir+"\\backup\\", homedir+"\\backup2\\")

    >
    > TIP: Use os.path.join(x,y, z*)
    >
    > > why is there still two \\ in the pathfor the copy command?

    >
    > I always convert my paths to use a single '/' instead of '\\'. Just
    > makes life that much easier!


    what does this mean? Use os.path.join(x,y, z*)
    what is the x,y,z?
     
    ecu_jon, Jan 30, 2011
    #3
  4. ecu_jon

    Chris Rebert Guest

    On Sun, Jan 30, 2011 at 3:13 PM, ecu_jon <> wrote:
    > On Jan 30, 3:55 pm, r <> wrote:
    >> On Jan 30, 2:44 pm, ecu_jon <> wrote:
    >>
    >> > shutil.copy (homedir+"\\backup\\", homedir+"\\backup2\\")

    >>
    >> TIP: Use os.path.join(x,y, z*)
    >>
    >> > why is there still two \\ in the pathfor the copy command?

    >>
    >> I always convert my paths to use a single '/' instead of '\\'. Just
    >> makes life that much easier!

    >
    > what does this mean?  Use os.path.join(x,y, z*)
    > what is the x,y,z?


    See http://docs.python.org/library/os.path.html#os.path.join
    e.g. in your case:
    backupdir = os.path.join(homedir, "backup")

    Cheers,
    Chris
     
    Chris Rebert, Jan 30, 2011
    #4
  5. ecu_jon

    rantingrick Guest

    On Jan 30, 5:13 pm, ecu_jon <> wrote:

    > what does this mean?  Use os.path.join(x,y, z*)
    > what is the x,y,z?


    x,y, and z in this case are just generic variables. Consider x+y=10. x
    and y could both equal 5 or any number of combinations of two numbers
    who sum equals ten. Anyway see the link chris posted to the docs or
    fire up your python shell and try this...


    >>> import sys
    >>> sys.version_info

    (2, 6, 5, 'final', 0) # yours may be different!
    >>> folder = 'C:/some\\\\path/to/folder'
    >>> filename = 'somefile.txt'
    >>> import os
    >>> help(os.path.join)

    Help on function join in module ntpath:

    join(a, *p)
    Join two or more pathname components, inserting "\" as needed.
    If any component is an absolute path, all previous path components
    will be discarded.

    >>> os.path.join(folder, filename)

    'C:/some\\\\path/to/folder\\somefile.txt'
    >>> help(os.path.normpath)

    Help on function normpath in module ntpath:

    normpath(path)
    Normalize path, eliminating double slashes, etc.

    >>> os.path.normpath(os.path.join(folder, filename))

    'C:\\some\\path\\to\\folder\\somefile.txt'

    psst: i know what you're thinking... and yes, python is very cool!
     
    rantingrick, Jan 30, 2011
    #5
  6. ecu_jon

    ecu_jon Guest

    ok now i get permission denied....

    import os
    homedir = os.path.expanduser('~')
    try:
    from win32com.shell import shellcon, shell
    homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    except ImportError:
    homedir = os.path.expanduser("~")
    print homedir
    print os.listdir(homedir+"\\backup\\")
    #homedir.replace("\\\\" , "\\")
    #print homedir
    backupdir1 = os.path.join(homedir, "backup")
    backupdir2 = os.path.join(homedir, "backup2")
    shutil.copy (backupdir1, backupdir2)
     
    ecu_jon, Jan 30, 2011
    #6
  7. ecu_jon

    MRAB Guest

    On 30/01/2011 23:43, ecu_jon wrote:
    > ok now i get permission denied....
    >
    > import os
    > homedir = os.path.expanduser('~')
    > try:
    > from win32com.shell import shellcon, shell
    > homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
    >
    > except ImportError:
    > homedir = os.path.expanduser("~")
    > print homedir
    > print os.listdir(homedir+"\\backup\\")
    > #homedir.replace("\\\\" , "\\")
    > #print homedir
    > backupdir1 = os.path.join(homedir, "backup")
    > backupdir2 = os.path.join(homedir, "backup2")
    > shutil.copy (backupdir1, backupdir2)
    >

    shutil.copy(...) copies files, not directories. You should use
    shutil.copytree(...) instead.
     
    MRAB, Jan 31, 2011
    #7
  8. ecu_jon

    ecu_jon Guest

    On Jan 30, 7:09 pm, MRAB <> wrote:
    > On 30/01/2011 23:43, ecu_jon wrote:
    >
    > > ok now i get permission denied....

    >
    > > import os
    > > homedir = os.path.expanduser('~')
    > > try:
    > >      from win32com.shell import shellcon, shell
    > >      homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    >
    > > except ImportError:
    > >      homedir = os.path.expanduser("~")
    > > print homedir
    > > print os.listdir(homedir+"\\backup\\")
    > > #homedir.replace("\\\\" , "\\")
    > > #print homedir
    > > backupdir1 = os.path.join(homedir, "backup")
    > > backupdir2 = os.path.join(homedir, "backup2")
    > > shutil.copy (backupdir1, backupdir2)

    >
    > shutil.copy(...) copies files, not directories. You should use
    > shutil.copytree(...) instead.


    i will, closer towards the end.
    just wanted to get past the naming stuff, the problem above.
    right now i just want to see it move files from 1 place to another.
    i had copytree in before, and will go back to that for final version.
    im working on a backup program, and copytree looks yummy.

    still no idea why getting permission denied.
     
    ecu_jon, Jan 31, 2011
    #8
  9. ecu_jon

    Dave Angel Guest

    On 01/-10/-28163 02:59 PM, ecu_jon wrote:
    > ok now i get permission denied....
    >
    > import os
    > homedir = os.path.expanduser('~')
    > try:
    > from win32com.shell import shellcon, shell
    > homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
    >
    > except ImportError:
    > homedir = os.path.expanduser("~")
    > print homedir
    > print os.listdir(homedir+"\\backup\\")
    > #homedir.replace("\\\\" , "\\")
    > #print homedir
    > backupdir1 = os.path.join(homedir, "backup")
    > backupdir2 = os.path.join(homedir, "backup2")
    > shutil.copy (backupdir1, backupdir2)
    >

    You forgot to include the error traceback.

    So, is homedir/backup a file, or is it a directory? If you're trying to
    copy whole directories, you might want to look at copytree instead.

    If you're not sure, you could use
    os.isfile()

    to check. If that's false, then shutil.copy() can't work. Similarly,
    if the destination is a readonly file, you'd get some error.

    DaveA
     
    Dave Angel, Jan 31, 2011
    #9
  10. ecu_jon

    ecu_jon Guest

    On Jan 30, 7:19 pm, Dave Angel <> wrote:
    > On 01/-10/-28163 02:59 PM, ecu_jon wrote:
    >
    > > ok now i get permission denied....

    >
    > > import os
    > > homedir = os.path.expanduser('~')
    > > try:
    > >      from win32com.shell import shellcon, shell
    > >      homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    >
    > > except ImportError:
    > >      homedir = os.path.expanduser("~")
    > > print homedir
    > > print os.listdir(homedir+"\\backup\\")
    > > #homedir.replace("\\\\" , "\\")
    > > #print homedir
    > > backupdir1 = os.path.join(homedir, "backup")
    > > backupdir2 = os.path.join(homedir, "backup2")
    > > shutil.copy (backupdir1, backupdir2)

    >
    > You forgot to include the error traceback.
    >
    > So, is homedir/backup a file, or is it a directory?  If you're trying to
    > copy whole directories, you might want to look at copytree instead.
    >
    > If you're not sure, you could use
    >      os.isfile()
    >
    > to check.  If that's false, then shutil.copy() can't work.  Similarly,
    > if the destination is a readonly file, you'd get some error.
    >
    > DaveA


    today's date is : 30
    week chosen is : 4
    C:\Users\jon
    ['test1.txt', 'test2.txt']

    Traceback (most recent call last):
    File "D:\spring 11\capstone-project\date.py", line 45, in <module>
    shutil.copy (backupdir1, backupdir2)
    File "C:\Python27\lib\shutil.py", line 116, in copy
    copyfile(src, dst)
    File "C:\Python27\lib\shutil.py", line 81, in copyfile
    with open(src, 'rb') as fsrc:
    IOError: [Errno 13] Permission denied: 'C:\\Users\\jon\\backup'
     
    ecu_jon, Jan 31, 2011
    #10
  11. ecu_jon

    rantingrick Guest

    On Jan 30, 5:43 pm, ecu_jon <> wrote:
    > ok now i get permission denied....


    [...]

    > shutil.copy (backupdir1, backupdir2)


    I must stress the importance of proper testing before ever running
    code that manipulates files! So many things can go wrong. Of course
    you are just copying files here and not deleting them however you must
    always be in the habit of treating files like explosives. And frankly
    you're being quite nonchalant with this very naive approach to coding
    and complete lack of testing.

    When handling files always test, test, test. Never actually move,
    copy, or delete until you are *absolutely* sure no failures will
    occur. I will always do test runs that print out the action but DO NOT
    actually DO the action, like...

    Copying files:
    -- from: C:\\somefile1
    to: C:\\blah\\somefile1
    -- from: C:\\somefile2
    to: C:\\blah\\somefile2
    -- from: C:\\somefile3
    to: C:\\blah\\somefile3
    -- etc...

    Once my test runs are bug free i can try to move or delete *one* file
    from some test set. Once that is bug free then i will try the code on
    many files of a test set, and ONLY THEN on the real thing. I guarantee
    if you keep manipulating files in such a haphazard way you will live
    to regret it!
     
    rantingrick, Jan 31, 2011
    #11
  12. ecu_jon

    ecu_jon Guest

    On Jan 30, 7:34 pm, rantingrick <> wrote:
    > On Jan 30, 5:43 pm, ecu_jon <> wrote:
    >
    > > ok now i get permission denied....

    >
    > [...]
    >
    > > shutil.copy (backupdir1, backupdir2)

    >
    > I must stress the importance of proper testing before ever running
    > code that manipulates files! So many things can go wrong. Of course
    > you are just copying files here and not deleting them however you must
    > always be in the habit of treating files like explosives. And frankly
    > you're being quite nonchalant with this very naive approach to coding
    > and complete lack of testing.
    >
    > When handling files always test, test, test. Never actually move,
    > copy, or delete until you are *absolutely* sure no failures will
    > occur. I will always do test runs that print out the action but DO NOT
    > actually DO the action, like...
    >
    > Copying files:
    >  -- from: C:\\somefile1
    >       to: C:\\blah\\somefile1
    >  -- from: C:\\somefile2
    >       to: C:\\blah\\somefile2
    >  -- from: C:\\somefile3
    >       to: C:\\blah\\somefile3
    >  -- etc...
    >
    > Once my test runs are bug free i can try to move or delete *one* file
    > from some test set. Once that is bug free then i will try the code on
    > many files of a test set, and ONLY THEN on the real thing. I guarantee
    > if you keep manipulating files in such a haphazard way you will live
    > to regret it!


    not nonchalant.
    i know i will ned to do testing and whatnot.
    just personally, i like to build stuff one concept at a time.
    for example, i had a problem with the homedir and peopel here helped
    me with that.
    now i have permissions problem, and an swer will likely meeerge.
    once i know how to copy a file, ill work on testing. like isfile and
    permissions.
    i know that has to be done. i guess its that you want tests before
    moves , and thats fine.
    since i have only ever had 1 python class, and basically learning this
    whole thing from scratch, i need to do things 1 step at a time.
    so now any thoughts on why i cannot write to my own homedir?
     
    ecu_jon, Jan 31, 2011
    #12
  13. ecu_jon

    MRAB Guest

    On 31/01/2011 00:18, ecu_jon wrote:
    > On Jan 30, 7:09 pm, MRAB<> wrote:
    >> On 30/01/2011 23:43, ecu_jon wrote:
    >>
    >>> ok now i get permission denied....

    >>
    >>> import os
    >>> homedir = os.path.expanduser('~')
    >>> try:
    >>> from win32com.shell import shellcon, shell
    >>> homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    >>
    >>> except ImportError:
    >>> homedir = os.path.expanduser("~")
    >>> print homedir
    >>> print os.listdir(homedir+"\\backup\\")
    >>> #homedir.replace("\\\\" , "\\")
    >>> #print homedir
    >>> backupdir1 = os.path.join(homedir, "backup")
    >>> backupdir2 = os.path.join(homedir, "backup2")
    >>> shutil.copy (backupdir1, backupdir2)

    >>
    >> shutil.copy(...) copies files, not directories. You should use
    >> shutil.copytree(...) instead.

    >
    > i will, closer towards the end.
    > just wanted to get past the naming stuff, the problem above.
    > right now i just want to see it move files from 1 place to another.
    > i had copytree in before, and will go back to that for final version.
    > im working on a backup program, and copytree looks yummy.
    >
    > still no idea why getting permission denied.


    The path given by backupdir1 is a directory, so you're trying to use
    shutil.copy(...) to copy a directory.

    The reason that it's complaining about permissions is that shutil.copy
    is trying to open the source file (look at the traceback) so that it
    can copy the file's contents, but it's not a file, it's a directory.

    CPython is written in C, so it's probably the underlying C library
    which is reporting it as a permissions problem instead of a "that's not
    a file!" problem. :)
     
    MRAB, Jan 31, 2011
    #13
  14. ecu_jon

    ecu_jon Guest

    On Jan 30, 8:25 pm, MRAB <> wrote:
    > On 31/01/2011 00:18, ecu_jon wrote:
    >
    >
    >
    > > On Jan 30, 7:09 pm, MRAB<>  wrote:
    > >> On 30/01/2011 23:43, ecu_jon wrote:

    >
    > >>> ok now i get permission denied....

    >
    > >>> import os
    > >>> homedir = os.path.expanduser('~')
    > >>> try:
    > >>>       from win32com.shell import shellcon, shell
    > >>>       homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    >
    > >>> except ImportError:
    > >>>       homedir = os.path.expanduser("~")
    > >>> print homedir
    > >>> print os.listdir(homedir+"\\backup\\")
    > >>> #homedir.replace("\\\\" , "\\")
    > >>> #print homedir
    > >>> backupdir1 = os.path.join(homedir, "backup")
    > >>> backupdir2 = os.path.join(homedir, "backup2")
    > >>> shutil.copy (backupdir1, backupdir2)

    >
    > >> shutil.copy(...) copies files, not directories. You should use
    > >> shutil.copytree(...) instead.

    >
    > > i will, closer towards the end.
    > > just wanted to get past the naming stuff, the problem above.
    > > right now i just want to see it move files from 1 place to another.
    > > i had copytree in before, and will go back to that for final version.
    > > im working on a backup program, and copytree looks yummy.

    >
    > > still no idea why getting permission denied.

    >
    > The path given by backupdir1 is a directory, so you're trying to use
    > shutil.copy(...) to copy a directory.
    >
    > The reason that it's complaining about permissions is that shutil.copy
    > is trying to open the source file (look at the traceback) so that it
    > can copy the file's contents, but it's not a file, it's a directory.
    >
    > CPython is written in C, so it's probably the underlying C library
    > which is reporting it as a permissions problem instead of a "that's not
    > a file!" problem. :)


    as for testing i was planning on using something like this
    http://docs.python.org/library/shutil.html
    scroll down to 10.10.1.1
     
    ecu_jon, Jan 31, 2011
    #14
  15. ecu_jon

    Dave Angel Guest

    On 01/-10/-28163 02:59 PM, ecu_jon wrote:
    > On Jan 30, 7:19 pm, Dave Angel<> wrote:
    >> On 01/-10/-28163 02:59 PM, ecu_jon wrote:
    >>
    >>> ok now i get permission denied....

    >>
    >>> import os
    >>> homedir =s.path.expanduser('~')
    >>> try:
    >>> from win32com.shell import shellcon, shell
    >>> homedir =hell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

    >>
    >>> except ImportError:
    >>> homedir =s.path.expanduser("~")
    >>> print homedir
    >>> print os.listdir(homedir+"\\backup\\")
    >>> #homedir.replace("\\\\" , "\\")
    >>> #print homedir
    >>> backupdir1 =s.path.join(homedir, "backup")
    >>> backupdir2 =s.path.join(homedir, "backup2")
    >>> shutil.copy (backupdir1, backupdir2)

    >>
    >> You forgot to include the error traceback.
    >>
    >> So, is homedir/backup a file, or is it a directory? If you're trying to
    >> copy whole directories, you might want to look at copytree instead.
    >>
    >> If you're not sure, you could use
    >> os.isfile()
    >>
    >> to check. If that's false, then shutil.copy() can't work. Similarly,
    >> if the destination is a readonly file, you'd get some error.
    >>
    >> DaveA

    >
    > today's date is : 30
    > week chosen is : 4
    > C:\Users\jon
    > ['test1.txt', 'test2.txt']
    >
    > Traceback (most recent call last):
    > File "D:\spring 11\capstone-project\date.py", line 45, in<module>
    > shutil.copy (backupdir1, backupdir2)
    > File "C:\Python27\lib\shutil.py", line 116, in copy
    > copyfile(src, dst)
    > File "C:\Python27\lib\shutil.py", line 81, in copyfile
    > with open(src, 'rb') as fsrc:
    > IOError: [Errno 13] Permission denied: 'C:\\Users\\jon\\backup'
    >


    Good job, reading the first part of my message. Now, why are you saying
    in other messages that it can't write to your home directory? The error
    doesn't refer to your home directory, it refers to a file
    C:\Users\jon\backup which it can't read.

    But as your program demonstrates, that's a directory not a file. It's
    fine to use shutil.copy, but then you have to give it a source file to copy.

    For example, C:\Users\jon\backup\test1.txt

    DaveA
     
    Dave Angel, Jan 31, 2011
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,237
  2. Replies:
    26
    Views:
    2,122
    Roland Pibinger
    Sep 1, 2006
  3. Replies:
    3
    Views:
    470
    Marcus Kwok
    Mar 1, 2007
  4. Florent 'flure' C.

    How to get homedir ?

    Florent 'flure' C., Apr 29, 2004, in forum: Ruby
    Replies:
    2
    Views:
    108
    Sascha Doerdelmann
    Apr 29, 2004
  5. MaggotChild

    File::Copy::copy With File Handles

    MaggotChild, Oct 18, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    507
    Ilya Zakharevich
    Oct 22, 2011
Loading...

Share This Page