Re: [Tutor] Problem with os.access function. [semantic error, if check does not work]
Hello, Thank you both for the tip's and the interesting links. I resolved my problem easily using another function, os.path.isdir(x) which was more specific. Best Regards. On 12/24/05, bob [EMAIL PROTECTED] wrote: At 05:20 AM 12/24/2005, Panagiotis Atmatzidis wrote: Hello, I am writing a function in order to check if a directory exists. If exists the functions must do nothing, otherwise must check the users permissions and if it's possible create the dir. Looking at pydoc's httpd I found the module os and the function access. From the http-doc: access(...) access(path, mode) - 1 if granted, 0 otherwise Use the real uid/gid to test for access to a path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to the path. The mode argument can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK. This is my function: def homedirhandle(): path = /some/dir/ # check the existance of the directory mode = 755 should be mode = 0755 (octal representation) for mkdir. For access: The mode argument can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK. suggests that only 1 digit is expected. check_path = os.access(path, mode) print check_path if check_path == 'False': Should be if check_path == False: Or even simpler if not check_path: Use print repr(check_path). Then you'd see either True or 'True'. That would help you see whether check_path is boolean or string. print print Directory /some/dir does not exist. print Trying to create the directory. uid = os.geteuid() print the uid is , uid if uid == '0': I think (not having UNIX access at the moment) that this should be if uid == 0: try: os.mkdir(path, mode) print print The directory has been created. print return path except OSError, e: print print sys.stderr, The mkdir command failed: %d (%s) % (e.errno, e.strerror) print print Exiting sys.exit(1) if check_path == '1': == 1 print print The directory /some/dir has been created. print return path else: print Please create the directory /some/dir manually and then re-run vuhalndler. print Exiting sys.exit() else: print print The directory already exists. print return path Now the problem lies at the first check if check_path == 'False': . It's a semantic error, the program does not really check the dir, it just takes for granted that the dir exists. I tried with 1 before putting False there.. but it did not work so I took the print result of check_path and substitute 1 with False. But still nothing :-( Why does not make the check? I thought that the functions functionality was clear.. probably is not. -- Panagiotis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Panagiotis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing
Thanks for the prompt reply. This is exactly what I am looking for. However, I have tried the code on the page and I can't get it to work. import tempfile import win32api filename = tempfile.mktemp (.txt) open (filename, w).write (This is a test) win32api.ShellExecute ( 0, print, filename, None, ., 0 ) I am using the Pythoncard code editor and I get the following error: Traceback (most recent call last): File c:\python24\jhc.py, line12, in ? 0 pywintypes.error: (2, 'ShellExecute', 'The system cannot find the file specified .') I have played about with it and saved it in various places but I can't get it to work. Any suggestions? Do I need to import other modules? Do I need to use Pythonwin? Thanks, John. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Danny Yoo Sent: 24 December 2005 19:33 To: John Corry Cc: Tutor Subject: Re: [Tutor] Printing I have downloaded win32, win32com, Preppy and PIL. I have had a go at using them but can't get them to work. At the moment I can't even print the text file. Is there a good helpguide/FAQ page which deals with printing text files or is there simple code which prints a text file? Hi John, Let's see... ok, found it! Tim Golden has written a small introduction to printing: http://tgolden.sc.sabren.com/python/win32_how_do_i/print.html His recommendation is to use the ShellExecute function in win32api to send off documents to your printer. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] regex
Does anyone see anything that jumps out at them on why these regex strings aren't catching this line: Dec 18 10:04:45 dragon logger: TCPWRAP: SERVICE=sshd@:::192.168.0.1,TYPE=ALL_DENY,HOST_ADDRESS=:::195.145.94.75,HOST_INFO=:::195.145.94.75,HOST_NAME=unknown,USER_NAME=unknown,OTHERINFO= This is the output of a tcpwrapper script I have, I am trying to write a script to parse this and tell me how many times host_address X has been denied access (among many other things). I have it working for the firewall rules just fine, but I am missing something somewhere to catch the string above. Below are the regex expressions I have tried: -- initial regex to find the line and pass it back to the routine to sort all this out. This line works, I believe (least I can get it to print all the entries back to me I am looking for. - rc('logger\S*\sTCPWRAP') : self.twist_failure Here is where I seem to run into trouble, none of the regex strings I have used seem to catch and sort out the strings. self.twist_fail_re = rc('SERVICE=\S*\sHOST_ADDRESS=\S*\sHOST_INFO=\S*\sHOST_NAME=\S*\sUSER_NAME=\S*\s') -- rc is set as rc = re.compile at the early part of my script. I have tried every combination I can think of for the _expression_ above, below are the couple I still have written down. self.twist_fail_re = rc('SERVICE=(\.)\.TYPE=(\.)\.HOST_ADDRESS=(\.)\.HOST_INFO=(\.)\.USER_NAME=(\.)') self.twist_fail_re = rc('SERVICE=(\S*)\S*TYPE=(\S*)\S*HOST_ADDRESS=(\S*)\S*HOST_INFO=(\S*)\S*USER_NAME=(\S*)') rc('SERVICE=\S*\sHOST_ADDRESS=\S*\sHOST_INFO=\S*\sHOST_NAME=\S*\sUSER_NAME=\S*\s') But for some reason they are not picking up the strings. Any suggestions? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor