Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2544 > unrolled thread

string to path problem

Started byecu_jon <hayesjdno3@yahoo.com>
First post2011-04-03 20:30 -0700
Last post2011-04-04 08:21 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  string to path problem ecu_jon <hayesjdno3@yahoo.com> - 2011-04-03 20:30 -0700
    Re: string to path problem Chris Rebert <clp2@rebertia.com> - 2011-04-03 21:17 -0700
      Re: string to path problem ecu_jon <hayesjdno3@yahoo.com> - 2011-04-03 21:18 -0700
        Re: string to path problem Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2011-04-04 14:36 +0530
          Re: string to path problem ecu_jon <hayesjdno3@yahoo.com> - 2011-04-04 08:21 -0700

#2544 — string to path problem

Fromecu_jon <hayesjdno3@yahoo.com>
Date2011-04-03 20:30 -0700
Subjectstring to path problem
Message-ID<7f0a2a35-8ae8-4625-9cef-8516b142cd25@f18g2000yqd.googlegroups.com>
i am writing a basic backup program for my school. so they wanted the
possibility to be able to set source/destination from a config file.
my source/destination was fine before, i would build it up with
functions, like 1 that got the user-name, and put it all together with
os.path.join. but if they set a source in the config file to something
like c:\users\jon\backup  python tries to read from c:\\users\\jon\
\backup, and throws out a read permission (because it doesn't
exist ...). i am not sure what to do at this point. i have look at the
docs for string, and os.____ . i cant os.path.join anything if they
give me an absolute path. here is a bit of the code

import os,string,fnmatch,shutil,time,getpass,md5,ConfigParser
from datetime import *
from os.path import join, getsize

config = ConfigParser.ConfigParser()
config.read("config.ini")
source = config.get("myvars", "source")
destination = config.get("myvars", "destination")
helptext = config.get("myvars", "helptext")

def weekChoice():
    dateNum = datetime.now().day
    if dateNum <=7:
        week = 1
    elif (dateNum >= 8) and (dateNum <= 14):
        week = 2
    elif (dateNum >= 15) and (dateNum <= 21):
        week = 3
    elif dateNum > 22:
        week = 4
    else:
        print "error"
    return week

def destination1():
    global destination
    username = getpass.getuser()
    week = weekChoice()
    weekstr = "week"+str(week)
    destination1 = os.path.join("//mothera","jon","week1") #where //
mothera is a samba server on the network
    return destination1
print "source :",source
destination = destination1()
shutil.copy2(source, destination)


here is some of config.ini
[myvars]
source:c:\users\jon\backup
destination:

[toc] | [next] | [standalone]


#2545

FromChris Rebert <clp2@rebertia.com>
Date2011-04-03 21:17 -0700
Message-ID<mailman.179.1301890636.2990.python-list@python.org>
In reply to#2544
On Sun, Apr 3, 2011 at 8:30 PM, ecu_jon <hayesjdno3@yahoo.com> wrote:
> i am writing a basic backup program for my school. so they wanted the
> possibility to be able to set source/destination from a config file.
> my source/destination was fine before, i would build it up with
> functions, like 1 that got the user-name, and put it all together with
> os.path.join. but if they set a source in the config file to something
> like c:\users\jon\backup  python tries to read from c:\\users\\jon\
> \backup, and throws out a read permission (because it doesn't
> exist ...).

Please give the exact error message and full exception traceback that
you're getting.

Cheers,
Chris

[toc] | [prev] | [next] | [standalone]


#2546

Fromecu_jon <hayesjdno3@yahoo.com>
Date2011-04-03 21:18 -0700
Message-ID<2939bffd-d4b5-453f-a5af-1dea9621e67f@k9g2000yqi.googlegroups.com>
In reply to#2545
On Apr 4, 12:17 am, Chris Rebert <c...@rebertia.com> wrote:
> On Sun, Apr 3, 2011 at 8:30 PM, ecu_jon <hayesjd...@yahoo.com> wrote:
> > i am writing a basic backup program for my school. so they wanted the
> > possibility to be able to set source/destination from a config file.
> > my source/destination was fine before, i would build it up with
> > functions, like 1 that got the user-name, and put it all together with
> > os.path.join. but if they set a source in the config file to something
> > like c:\users\jon\backup  python tries to read from c:\\users\\jon\
> > \backup, and throws out a read permission (because it doesn't
> > exist ...).
>
> Please give the exact error message and full exception traceback that
> you're getting.
>
> Cheers,
> Chris
Traceback (most recent call last):
  File "I:\college\spring11\capstone-project\testing1.py", line 39, in
<module>
    shutil.copy2(source1, destination)
  File "C:\Python27\lib\shutil.py", line 127, in copy2
    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'

i have permission to c:\users\jon\*
but c:\\* obviously does not exist.

[toc] | [prev] | [next] | [standalone]


#2558

FromKushal Kumaran <kushal.kumaran+python@gmail.com>
Date2011-04-04 14:36 +0530
Message-ID<mailman.0.1301908032.15055.python-list@python.org>
In reply to#2546
On Mon, Apr 4, 2011 at 9:48 AM, ecu_jon <hayesjdno3@yahoo.com> wrote:
> On Apr 4, 12:17 am, Chris Rebert <c...@rebertia.com> wrote:
>> On Sun, Apr 3, 2011 at 8:30 PM, ecu_jon <hayesjd...@yahoo.com> wrote:
>> > i am writing a basic backup program for my school. so they wanted the
>> > possibility to be able to set source/destination from a config file.
>> > my source/destination was fine before, i would build it up with
>> > functions, like 1 that got the user-name, and put it all together with
>> > os.path.join. but if they set a source in the config file to something
>> > like c:\users\jon\backup  python tries to read from c:\\users\\jon\
>> > \backup, and throws out a read permission (because it doesn't
>> > exist ...).
>>
>> Please give the exact error message and full exception traceback that
>> you're getting.
>>
>> Cheers,
>> Chris
> Traceback (most recent call last):
>  File "I:\college\spring11\capstone-project\testing1.py", line 39, in
> <module>
>    shutil.copy2(source1, destination)
>  File "C:\Python27\lib\shutil.py", line 127, in copy2
>    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'
>
> i have permission to c:\users\jon\*
> but c:\\* obviously does not exist.

The extra backslashes in the string literal are there to "escape" the
required backslashes.  This is required because the backslash
character is used to introduce certain special characters in strings,
such as tabs and newlines.  The actual string does not contain the
extra backslashes.  This is documented in extensive detail in the
language reference:
http://docs.python.org/reference/lexical_analysis.html#string-literals.
 But you might want to start with the tutorial:
http://docs.python.org/tutorial/introduction.html#strings

Example:

>>> s = 'c:\\users\\jon\\backup'
>>> print s
c:\users\jon\backup
>>>

Is c:\users\jon\backup a directory?  The shutil.copyfile function will
only copy a file.  There is a shutil.copytree that will copy an entire
directory tree.

-- 
regards,
kushal

[toc] | [prev] | [next] | [standalone]


#2569

Fromecu_jon <hayesjdno3@yahoo.com>
Date2011-04-04 08:21 -0700
Message-ID<0b16cef8-c604-469b-9d9c-feb2198af21d@hg8g2000vbb.googlegroups.com>
In reply to#2558
On Apr 4, 5:06 am, Kushal Kumaran <kushal.kumaran+pyt...@gmail.com>
wrote:
> On Mon, Apr 4, 2011 at 9:48 AM, ecu_jon <hayesjd...@yahoo.com> wrote:
> > On Apr 4, 12:17 am, Chris Rebert <c...@rebertia.com> wrote:
> >> On Sun, Apr 3, 2011 at 8:30 PM, ecu_jon <hayesjd...@yahoo.com> wrote:
> >> > i am writing a basic backup program for my school. so they wanted the
> >> > possibility to be able to set source/destination from a config file.
> >> > my source/destination was fine before, i would build it up with
> >> > functions, like 1 that got the user-name, and put it all together with
> >> > os.path.join. but if they set a source in the config file to something
> >> > like c:\users\jon\backup  python tries to read from c:\\users\\jon\
> >> > \backup, and throws out a read permission (because it doesn't
> >> > exist ...).
>
> >> Please give the exact error message and full exception traceback that
> >> you're getting.
>
> >> Cheers,
> >> Chris
> > Traceback (most recent call last):
> >  File "I:\college\spring11\capstone-project\testing1.py", line 39, in
> > <module>
> >    shutil.copy2(source1, destination)
> >  File "C:\Python27\lib\shutil.py", line 127, in copy2
> >    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'
>
> > i have permission to c:\users\jon\*
> > but c:\\* obviously does not exist.
>
> The extra backslashes in the string literal are there to "escape" the
> required backslashes.  This is required because the backslash
> character is used to introduce certain special characters in strings,
> such as tabs and newlines.  The actual string does not contain the
> extra backslashes.  This is documented in extensive detail in the
> language reference:http://docs.python.org/reference/lexical_analysis.html#string-literals.
>  But you might want to start with the tutorial:http://docs.python.org/tutorial/introduction.html#strings
>
> Example:
>
>
>
> >>> s = 'c:\\users\\jon\\backup'
> >>> print s
> c:\users\jon\backup
>
> Is c:\users\jon\backup a directory?  The shutil.copyfile function will
> only copy a file.  There is a shutil.copytree that will copy an entire
> directory tree.
>
> --
> regards,
> kushal

well i changed a few minor things in the bigger problem, and c:\users
\jon\backup as the source worked fine. now to test it on winxp, where
the default has a space in the name.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web