Python Script Cannot Write to Directory

C

Chris Brauchli

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
 
A

Albert Hopkins

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?
 
N

News123

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
 
M

Matteo Landi

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.
 
S

Steven D'Aprano

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 :)
 
N

News123

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top