homedir, file copy

E

ecu_jon

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

r

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

ecu_jon

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


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

rantingrick

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

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.
Help on function normpath in module ntpath:

normpath(path)
Normalize path, eliminating double slashes, etc.
'C:\\some\\path\\to\\folder\\somefile.txt'

psst: i know what you're thinking... and yes, python is very cool!
 
E

ecu_jon

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

MRAB

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

ecu_jon

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

Dave Angel

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
 
E

ecu_jon

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

rantingrick

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

ecu_jon

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

MRAB

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

ecu_jon

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
 
D

Dave Angel

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
 

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,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top