![]() |
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? |
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! |
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? |
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 |
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! |
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) |
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. |
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. |
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 |
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.