Python Script Cannot Write to Directory

Discussion in 'Python' started by Chris Brauchli, Aug 4, 2010.

  1. Hi,

    I am writing a script that, at one point, copies a file from directory
    A to directory B. Directory B can only be written to by root, but the
    script is always called with sudo, so this shouldn't be an issue, but
    it is. I have tried using shutil.copy() and calling "sudo cp " with
    os.popen to no avail. I cannot get the script to copy a file to
    directory B. The strange thing is if I run the python interpreter (as
    sudo) and type in shutil.copy it works. It also works if I try to copy
    the file to a less protected directory. It only happens when I try to
    copy a file to directory B from a python script.

    Any ideas why this is happening? If more information is needed or
    something isn't clear let me know. Thanks for helping.

    Chris
    Chris Brauchli, Aug 4, 2010
    #1
    1. Advertising

  2. On Tue, 2010-08-03 at 21:01 -0700, Chris Brauchli wrote:
    > Hi,
    >
    > I am writing a script that, at one point, copies a file from directory
    > A to directory B. Directory B can only be written to by root, but the
    > script is always called with sudo, so this shouldn't be an issue, but
    > it is. I have tried using shutil.copy() and calling "sudo cp " with
    > os.popen to no avail. I cannot get the script to copy a file to
    > directory B. The strange thing is if I run the python interpreter (as
    > sudo) and type in shutil.copy it works. It also works if I try to copy
    > the file to a less protected directory. It only happens when I try to
    > copy a file to directory B from a python script.
    >
    > Any ideas why this is happening? If more information is needed or
    > something isn't clear let me know. Thanks for helping.
    >
    > Chris


    Code?

    Traceback?
    Albert Hopkins, Aug 4, 2010
    #2
    1. Advertising

  3. Chris Brauchli

    News123 Guest

    On 08/04/2010 06:01 AM, Chris Brauchli wrote:
    > Hi,
    >
    > I am writing a script that, at one point, copies a file from directory
    > A to directory B. Directory B can only be written to by root, but the
    > script is always called with sudo, so this shouldn't be an issue, but
    > it is. I have tried using shutil.copy() and calling "sudo cp " with
    > os.popen to no avail. I cannot get the script to copy a file to
    > directory B. The strange thing is if I run the python interpreter (as
    > sudo) and type in shutil.copy it works. It also works if I try to copy
    > the file to a less protected directory. It only happens when I try to
    > copy a file to directory B from a python script.
    >
    > Any ideas why this is happening? If more information is needed or
    > something isn't clear let me know. Thanks for helping.
    >


    a fiew suggestions suggestions:


    1,) open a terminal window with sudo -s and make ALL of the following
    tests from this window.

    2.) tell use the exact permissions of your destination directory
    ls -ld DESTINATION_DIRECTORY

    3.) verify, whether the file you want to copy exists already in the
    destination directory
    ls -l DESTINATION_DIRECTORY/DSTNTN_FILE_NAME


    3.) try following python

    import os
    print os.getcwd()
    import shutil
    shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME")


    4.) try the same from a shell window
    cp YOUR_SOURCE_FILE_NAME DESTINATION_DIRECTORY/DSTNTN_FILE_NAME
    News123, Aug 4, 2010
    #3
  4. Chris Brauchli

    Chris Rebert Guest

    On Wed, Aug 4, 2010 at 12:21 AM, News123 <> wrote:
    <snip>
    > 3.) try following python
    >
    > import os
    > print os.getcwd()
    > import shutil
    > shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME")


    WTF; modules aren't callable. Typo?

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Aug 4, 2010
    #4
  5. Chris Brauchli

    Matteo Landi Guest

    On Wed, Aug 4, 2010 at 9:27 AM, Chris Rebert <> wrote:
    > On Wed, Aug 4, 2010 at 12:21 AM, News123 <> wrote:
    > <snip>
    >> 3.) try following python
    >>
    >> import os
    >> print os.getcwd()
    >> import shutil
    >> shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME")

    >
    > WTF; modules aren't callable. Typo?


    I suppose he/she would have written:

    shutil.copyfile("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME")

    Cheers.

    >
    > Cheers,
    > Chris
    > --
    > http://blog.rebertia.com
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Matteo Landi
    http://www.matteolandi.net/
    Matteo Landi, Aug 4, 2010
    #5
  6. On Tue, 03 Aug 2010 21:01:38 -0700, Chris Brauchli wrote:

    > Hi,
    >
    > I am writing a script that, at one point, copies a file from directory A
    > to directory B. Directory B can only be written to by root, but the
    > script is always called with sudo, so this shouldn't be an issue, but it
    > is. I have tried using shutil.copy() and calling "sudo cp " with
    > os.popen to no avail. I cannot get the script to copy a file to
    > directory B.

    [...]
    > Any ideas why this is happening? If more information is needed or
    > something isn't clear let me know. Thanks for helping.


    Without seeing the traceback and the actual line of code that fails, and
    preferably the full set of permissions on the directory, we'd be
    guessing. Now, I love to play "debug the program by making wild guesses"
    with other people's code, but you might not like my suggestions :)



    --
    Steven
    Steven D'Aprano, Aug 4, 2010
    #6
  7. Chris Brauchli

    News123 Guest

    On 08/04/2010 09:27 AM, Chris Rebert wrote:
    > On Wed, Aug 4, 2010 at 12:21 AM, News123 <> wrote:
    > <snip>
    >> 3.) try following python
    >>
    >> import os
    >> print os.getcwd()
    >> import shutil
    >> shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME")

    >
    > WTF; modules aren't callable. Typo?
    >


    Indeed. This is a typo and Matteo Landi's right about what I meant:
    shutil.copyfile("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME")


    What remains is:

    I think the answers to my questions will help to reveal the cause of the
    problem.


    The original question lacks necessary information
    as file permissions / directory permissions etc. / the exact error message.
    News123, Aug 5, 2010
    #7
  8. In message <4c5a6d00$0$29614$>, News123 wrote:

    > The original question lacks necessary information


    That seems to be very common in this newsgroup.
    Lawrence D'Oliveiro, Aug 6, 2010
    #8
  9. Lawrence D'Oliveiro wrote:
    > In message <4c5a6d00$0$29614$>, News123 wrote:
    >
    >
    >> The original question lacks necessary information
    >>

    >
    > That seems to be very common in this newsgroup.
    >

    ....

    JM
    Jean-Michel Pichavant, Aug 6, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Gao
    Replies:
    0
    Views:
    1,207
    J Gao
    Jul 28, 2003
  2. Marcia Hon
    Replies:
    0
    Views:
    448
    Marcia Hon
    Feb 8, 2004
  3. Mike Wahler
    Replies:
    0
    Views:
    407
    Mike Wahler
    Feb 8, 2004
  4. Martin Ambuhl
    Replies:
    0
    Views:
    382
    Martin Ambuhl
    Feb 8, 2004
  5. Replies:
    6
    Views:
    1,565
    David Bolen
    Mar 1, 2005
Loading...

Share This Page