Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   homedir, file copy (http://www.velocityreviews.com/forums/t742837-homedir-file-copy.html)

ecu_jon 01-30-2011 08:44 PM

homedir, file copy
 
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 01-30-2011 08:55 PM

Re: homedir, file copy
 
On Jan 30, 2:44*pm, ecu_jon <hayesjd...@yahoo.com> 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!


ecu_jon 01-30-2011 11:13 PM

Re: homedir, file copy
 
On Jan 30, 3:55*pm, r <rt8...@gmail.com> wrote:
> On Jan 30, 2:44*pm, ecu_jon <hayesjd...@yahoo.com> 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?

Chris Rebert 01-30-2011 11:23 PM

Re: homedir, file copy
 
On Sun, Jan 30, 2011 at 3:13 PM, ecu_jon <hayesjdno3@yahoo.com> wrote:
> On Jan 30, 3:55Â*pm, r <rt8...@gmail.com> wrote:
>> On Jan 30, 2:44Â*pm, ecu_jon <hayesjd...@yahoo.com> 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.pa...l#os.path.join
e.g. in your case:
backupdir = os.path.join(homedir, "backup")

Cheers,
Chris

rantingrick 01-30-2011 11:36 PM

Re: homedir, file copy
 
On Jan 30, 5:13*pm, ecu_jon <hayesjd...@yahoo.com> 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!

ecu_jon 01-30-2011 11:43 PM

Re: homedir, file copy
 
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)


MRAB 01-31-2011 12:09 AM

Re: homedir, file copy
 
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.

ecu_jon 01-31-2011 12:18 AM

Re: homedir, file copy
 
On Jan 30, 7:09*pm, MRAB <pyt...@mrabarnett.plus.com> 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.

Dave Angel 01-31-2011 12:19 AM

Re: homedir, file copy
 
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




ecu_jon 01-31-2011 12:27 AM

Re: homedir, file copy
 
On Jan 30, 7:19*pm, Dave Angel <da...@ieee.org> 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'


All times are GMT. The time now is 08:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57