Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Script Cannot Write to Directory

Reply
Thread Tools

Python Script Cannot Write to Directory

 
 
Chris Brauchli
Guest
Posts: n/a
 
      08-04-2010
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
 
Reply With Quote
 
 
 
 
Albert Hopkins
Guest
Posts: n/a
 
      08-04-2010
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?

 
Reply With Quote
 
 
 
 
News123
Guest
Posts: n/a
 
      08-04-2010
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_DIRECT ORY/DSTNTN_FILE_NAME")


4.) try the same from a shell window
cp YOUR_SOURCE_FILE_NAME DESTINATION_DIRECTORY/DSTNTN_FILE_NAME


 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      08-04-2010
On Wed, Aug 4, 2010 at 12:21 AM, News123 <(E-Mail Removed)> wrote:
<snip>
> 3.) try following python
>
> import os
> print os.getcwd()
> import shutil
> shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECT ORY/DSTNTN_FILE_NAME")


WTF; modules aren't callable. Typo?

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Matteo Landi
Guest
Posts: n/a
 
      08-04-2010
On Wed, Aug 4, 2010 at 9:27 AM, Chris Rebert <(E-Mail Removed)> wrote:
> On Wed, Aug 4, 2010 at 12:21 AM, News123 <(E-Mail Removed)> wrote:
> <snip>
>> 3.) try following python
>>
>> import os
>> print os.getcwd()
>> import shutil
>> shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECT ORY/DSTNTN_FILE_NAME")

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


I suppose he/she would have written:

shutil.copyfile("YOUR_SOURCE_FILE_NAME","DESTINATI ON_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/
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-04-2010
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
 
Reply With Quote
 
News123
Guest
Posts: n/a
 
      08-05-2010
On 08/04/2010 09:27 AM, Chris Rebert wrote:
> On Wed, Aug 4, 2010 at 12:21 AM, News123 <(E-Mail Removed)> wrote:
> <snip>
>> 3.) try following python
>>
>> import os
>> print os.getcwd()
>> import shutil
>> shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECT ORY/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","DESTINATI ON_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.


 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      08-06-2010
In message <4c5a6d00$0$29614$(E-Mail Removed)>, News123 wrote:

> The original question lacks necessary information


That seems to be very common in this newsgroup.
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      08-06-2010
Lawrence D'Oliveiro wrote:
> In message <4c5a6d00$0$29614$(E-Mail Removed)>, News123 wrote:
>
>
>> The original question lacks necessary information
>>

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

....

JM


 
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
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
cannot open file in write mode, no such file or directory haynesc@gmail.com Python 6 03-01-2005 04:17 PM
Server cannot access application directory... The directory does not exist or is not accessible because of security settings. ASP .Net Security 1 05-21-2004 03:47 PM
Re: Cannot write file, cannot do variable length packets Mike Wahler C Programming 0 02-08-2004 09:18 PM
cannot write file, cannot do variable length packet Marcia Hon C Programming 0 02-08-2004 09:01 PM



Advertisments