Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > homedir, file copy

Reply
Thread Tools

homedir, file copy

 
 
ecu_jon
Guest
Posts: n/a
 
      01-30-2011
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?
 
Reply With Quote
 
 
 
 
r
Guest
Posts: n/a
 
      01-30-2011
On Jan 30, 2:44*pm, ecu_jon <(E-Mail Removed)> 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!

 
Reply With Quote
 
 
 
 
ecu_jon
Guest
Posts: n/a
 
      01-30-2011
On Jan 30, 3:55*pm, r <(E-Mail Removed)> wrote:
> On Jan 30, 2:44*pm, ecu_jon <(E-Mail Removed)> 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?
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      01-30-2011
On Sun, Jan 30, 2011 at 3:13 PM, ecu_jon <(E-Mail Removed)> wrote:
> On Jan 30, 3:55*pm, r <(E-Mail Removed)> wrote:
>> On Jan 30, 2:44*pm, ecu_jon <(E-Mail Removed)> 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
 
Reply With Quote
 
rantingrick
Guest
Posts: n/a
 
      01-30-2011
On Jan 30, 5:13*pm, ecu_jon <(E-Mail Removed)> 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!
 
Reply With Quote
 
ecu_jon
Guest
Posts: n/a
 
      01-30-2011
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)

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      01-31-2011
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.
 
Reply With Quote
 
ecu_jon
Guest
Posts: n/a
 
      01-31-2011
On Jan 30, 7:09*pm, MRAB <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      01-31-2011
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



 
Reply With Quote
 
ecu_jon
Guest
Posts: n/a
 
      01-31-2011
On Jan 30, 7:19*pm, Dave Angel <(E-Mail Removed)> 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'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File::Copy::copy With File Handles MaggotChild Perl Misc 2 10-22-2011 12:15 AM
unicode shutil.copy() changes a file name during copy? dave Python 6 02-16-2011 09:53 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
using File.Copy to copy files to shared hosting site Steve Richter ASP .Net 4 04-18-2005 03:06 PM



Advertisments