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 10 2515
Rigga wrote: 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
> 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"
--
Diez
> #!/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..."
Alex Martelli wrote: Rigga wrote:
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
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
On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Ri***@noemail.com> wrote: 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?
--
Christopher
Christopher Koppler wrote: On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Ri***@noemail.com> wrote:
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?
-- Christopher
because Ive assigned it using the FilePath = os.path('/home/rigga') -
surely therefore FilePath contains the value /home/rigga???????
On Sat, 11 Oct 2003 16:08:46 +0000, Rigga <Ri***@noemail.com> wrote: Christopher Koppler wrote:
On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Ri***@noemail.com> wrote:
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?
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.
--
Christopher
On Sat, 11 Oct 2003 15:22:36 GMT, Christopher Koppler
<kl******@chello.at> wrote: On Sat, 11 Oct 2003 16:08:46 +0000, Rigga <Ri***@noemail.com> wrote:
Christopher Koppler wrote:
On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Ri***@noemail.com> wrote:
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?
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.
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.
--
Christopher
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
--
Cousin Stanley
Human Being
Phoenix, Arizona
Christopher Koppler wrote: On Sat, 11 Oct 2003 15:22:36 GMT, Christopher Koppler <kl******@chello.at> wrote:
On Sat, 11 Oct 2003 16:08:46 +0000, Rigga <Ri***@noemail.com> wrote:
Christopher Koppler wrote:
On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Ri***@noemail.com> wrote:
>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?
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.
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.
-- Christopher
Thank you!! it makes more sense to me now.
Cheers
Rigga This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rigga |
last post by:
Hi
I would like to pass a variable to an external program i.e.
os.system('cd $variable')
However this doesnt work, I cant figure it out, any ideas?
Cheers
|
by: MC |
last post by:
Hi to all,
I would like to use in a XSL stylesheet an array variable which is
defined in a Java program.
For example, in my program, i defined a tab variable
String tab = {"first", "second",...
|
by: Cathie |
last post by:
Hi All,
I am trying to get my style sheet to work. It works fine in IE but I can't
get it to work in .net.
Below is the function I use for transforming, where advancedOptionsFile is
the path...
|
by: Rich |
last post by:
I want to run an external program using os.system() but I want to
include a variable in the middle of the command line.
An example of the type of thing I want to be able to do:
pathname =...
|
by: Roman Mashak |
last post by:
Hello,
I have a small piece of code, compiled by two 'gcc' and 'borland builder
compiler'. The latter one produces warnings:
Public symbol '_freq' defined in both module...
|
by: istillshine |
last post by:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions...
|
by: whirlwindkevin |
last post by:
I saw a program source code in which a variable is defined in a header
file and that header file is included in 2 different C files.When i
compile and link the files no error is being thrown.How is...
|
by: Jaco Naude |
last post by:
Hi,
I'm using a static library in my application which links fine except
for a few global variables. The static library only contains a bunch
of .cpp and .h files and the global variables are...
|
by: Christian Meier |
last post by:
Hello NG
I have the following code:
file1.h:
static const int iValue = 5;
<EOF>
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |