Variable passing to external program - How??

A

Alex Martelli

Rigga said:
Hi,

Ok first please bear with me as I am a total Python n00b..

OK, but: the post's subject bears no relation to your code; and
neither does your text -- you keep talking about an os.system
call that just isn't there. So, "noob" or not, I'm nonplussed.
Can anyone explain why this does not like me using % FileLoc in the
os.system call???

There is no os.system call in the following code.
#!/usr/bin/python
import sys
import os
# Check that the folder is accessible and writeable
FileLoc = os.path.exists('/home/rigga')
if (FileLoc):
print "File location exists:"
AccFlag = os.access('% FileLoc',os.R_OK | os.X_OK | os.W_OK)

You're checking for a file called '% FileLoc', which does not exist. The
variable FileLoc at this point is worth True, so you can't possibly want
to "pass it to an external program" as per subject, either.
I have full access to the folder I am checking however it always returns
no files found (FileLoc = 0) however if I specify the folder I want to
test in the os.system call it works fine...

There is no os.system call anywhere in the above.


Alex
 
R

Rigga

Hi,

Ok first please bear with me as I am a total Python n00b..

Can anyone explain why this does not like me using % FileLoc in the
os.system call???

#!/usr/bin/python
import sys
import os
# Check that the folder is accessible and writeable
FileLoc = os.path.exists('/home/rigga')
if (FileLoc):
print "File location exists:"
AccFlag = os.access('% FileLoc',os.R_OK | os.X_OK | os.W_OK)
if (AccFlag):
print "You have FULL access to the location"
else:
print "**Error - You do not have access to the location**"
else:
print "No files found exiting..."

sys.exit()

I have full access to the folder I am checking however it always returns no
files found (FileLoc = 0) however if I specify the folder I want to test in
the os.system call it works fine...

Any help appreciated

Cheerz

Rigga
 
D

Diez B. Roggisch

FileLoc = os.path.exists('/home/rigga')

This yields true or false, depending on the existence of "/home/rigga"
I'm guessing now, but what you want is this:

FileLoc = "/home/rigga"
if os.path.exists(FileLoc):
....
if (FileLoc):
print "File location exists:"
AccFlag = os.access('% FileLoc',os.R_OK | os.X_OK | os.W_OK)

Guessing again - you want to check your perms on that FileLoc of yours - so
why don't you just pass it into the function?
AccFlag = os.access(FileLoc,os.R_OK | os.X_OK | os.W_OK)

The % operator works similar to printf in C/PHP/Whatever. If you absolutely
want it here, this would work:
print "%s" % FileLoc
"/home/rigga"
 
L

Lee Harr

#!/usr/bin/python
import sys
import os
# Check that the folder is accessible and writeable
FileLoc = os.path.exists('/home/rigga')
if (FileLoc):
print "File location exists:"
AccFlag = os.access('% FileLoc',os.R_OK | os.X_OK | os.W_OK)
if (AccFlag):
print "You have FULL access to the location"
else:
print "**Error - You do not have access to the location**"
else:
print "No files found exiting..."

sys.exit()

import os
FileLoc = '/home/rigga'

if os.path.exists(FileLoc):
print "File location exists"
AccFlag = os.access(FileLoc, os.R_OK | os.X_OK | os.W_OK)
if AccFlag:
print "You have FULL access to the location"
else:
print "**Error - You do not have access to the location**"
else:
print "No files found exiting..."
 
C

Christopher Koppler

FilePath = os.path('/home/rigga')
AccFlag = os.access('% FilePath',os.R_OK)

I would expect % FilePath to contain /home/rigga

Why would you expect that?
 
C

Christopher Koppler

because Ive assigned it using the FilePath = os.path('/home/rigga') -
surely therefore FilePath contains the value /home/rigga???????

Yes, but what do you think that '% FilePath' means? This is an
ordinary string, and does not magically expand to the variable's
contents, which I assume you wanted. You need to use just the variable
name for that:

AccFlag = os.access(FilePath, os.R_OK)

If you wanted to use the % operator for strings, you could also write
that as

AccFlag = os.access('%s' % FilePath, os.R_OK)

which is completely unnecessary in this case, however.
 
C

Christopher Koppler

Yes, but what do you think that '% FilePath' means? This is an
ordinary string, and does not magically expand to the variable's
contents, which I assume you wanted. You need to use just the variable
name for that:

AccFlag = os.access(FilePath, os.R_OK)

If you wanted to use the % operator for strings, you could also write
that as

AccFlag = os.access('%s' % FilePath, os.R_OK)

which is completely unnecessary in this case, however.

And also, I completely overlooked:
FilePath = os.path('/home/rigga') will not work either, because
os.path is a _module_ (which is not callable), not a function to
create paths. Pathnames are just strings, so

FilePath = '/home/rigga'

is what you want.
 
R

Rigga

Alex said:
OK, but: the post's subject bears no relation to your code; and
neither does your text -- you keep talking about an os.system
call that just isn't there. So, "noob" or not, I'm nonplussed.


There is no os.system call in the following code.


You're checking for a file called '% FileLoc', which does not exist. The
variable FileLoc at this point is worth True, so you can't possibly want
to "pass it to an external program" as per subject, either.


There is no os.system call anywhere in the above.


Alex
Alex, point taken I rushed when I was doing this and made many mistakes,
thatll learn me not to rush a post!.

To all the others that replied thanks for your input it has helped me a lot.

What I was going to add to the original post but forgot was how you pass
variables to external programs - hence the title. I have been reading
though the Python Bible however all this variable passing doesnt appear to
work as I would expect i.e.

FilePath = os.path('/home/rigga')
AccFlag = os.access('% FilePath',os.R_OK)

I would expect % FilePath to contain /home/rigga and for os.access to parse
% FilePath in to that and return the results.... however it doesnt, it only
works if I specify the directory in os.access...

Or am I just being stupid?

Cheerz

Rigga
 
R

Rigga

Christopher said:
Why would you expect that?
because Ive assigned it using the FilePath = os.path('/home/rigga') -
surely therefore FilePath contains the value /home/rigga???????
 
C

Cousin Stanley

Following is yet another version for checking file access
that takes the file_path as an argument to the module,
and differentiates R,E,W or NO access ....

'''
Module ....... os_file_access.py
NewsGroup .... comp.lang.python
Date ......... 2003-10-10
Posted_By .... rigga
Edited_By .... Stanley C. Kitching
'''

import os
import sys

NL = '\n'
SP = ' '
SP3 = SP * 3
SP7 = SP * 7

module_this = sys.argv[ 0 ]

print '%s %s ' % ( NL , module_this ) , NL

if len( sys.argv ) < 2 :

print SP7 , 'Usage : python os_file_access.py file_path' , NL

sys.exit( -1 )

file_path = sys.argv[ 1 ] # Input File Path as 1st Argument

if os.path.exists( file_path ) :

print SP7 , "File Path Exists ...." , file_path , NL

read_flag = os.access( file_path , os.R_OK )
exec_flag = os.access( file_path , os.X_OK )
write_flag = os.access( file_path , os.W_OK )

else :

print SP7 , "*** File NOT Found ***" , NL
print SP7 , SP3 , file_path , NL

sys.exit( -2 )

list_access = []

if read_flag : list_access.append( 'Read' )
if exec_flag : list_access.append( 'Execute' )
if write_flag : list_access.append( 'Write' )

if ( read_flag | exec_flag | write_flag ) :

str_access = ' , '.join( list_access )

else :

str_access = 'NO'

print SP7 , 'You have [ %s ] Access to the File' % ( str_access ) , NL
 
R

Rigga

Christopher said:
And also, I completely overlooked:
FilePath = os.path('/home/rigga') will not work either, because
os.path is a _module_ (which is not callable), not a function to
create paths. Pathnames are just strings, so

FilePath = '/home/rigga'

is what you want.
Thank you!! it makes more sense to me now.

Cheers

Rigga
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top