[Tutor] Problem with os.access function. [semantic error, if check does not work]
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 check_path = os.access(path, mode) print check_path if check_path == 'False': 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': 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': 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
Re: [Tutor] Learning books
Okay, so I have been reading some of the tutorials around the net on Python. great stuff I might add but I am getting all confused with the TCL, xwwidgets etc. I want to be able to program and I am just using the standard IDE that comes with Python. Am I on the right track? Yes, when beginning stick to the most basic tools. The wxWidgets stuff is the underbelly of wxPython but you should only need to worry about that once you get into quite deep GUI design. Similarly Tcl/Tk is the underbelly of Tkinter and again you should rarely ever see Tcl nowadays in Python, apart from a deeply nested error message from Tkinter - where you can nearly always ignore the Tcl aspects! Other languages are useful to compare with, seeing the same basic structures in different languages can emphasise that the concepts are the same, it's only syntax that changes. But only try to *learn* one lot of syntax at a time, otherwise you will get confused. with C++ but heck, I just want to learn Python for now. I do want the widgets to look nice sure. HELP! wxWidgets is written in C++, as are most native code applications etc. Unfortunately most of the documentation for wxPython still uses the C++ documents so you either have to work from the higher level Python documents or learn to read (at a superficial level) the C++ documents. To do that you really need to have covered the basics of OOP - which is one thing I like about Tkinter, you don't need OOP to use it, although OOP makes it easier... But as a beginner most of your programs should be targetted at the command line. To try to build GUIs too soon will simply distract from the fundamental proinciples of programming. And once you have a working command-line version its usually fairly easy to convert it for GUI use later, especially if you keep the display functions (ie. print statements) separate from the logic. HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Input checking [letters or numbers]
Another newbe question! I use while True: to evaluate an expression, I see that you used while 1: .. what's the diffrence if any?! Python only provided boolean literal values (True, False) relatively recently. Long time Python programmers, especially those with a C background(*) are used to writing 1 to mean True. It's a hard habit to break. But it means exactly the same as True and since True is usually more readable its probably better to use that. (In theory Python could change to make 1 and True non compatible, but in truth thats extremely unlikely because it is such a deeply embedded tradition and huge amounts of existing code would break!) (*) Strictly speaking the numeric definition is based on 0 being False and True being non-zero. Thus C programmers used to do this: #define FALSE 0 #define TRUE !FALSE This left the numeric value of TRUE up to the compiler, in most cases it would be 1 but in some cases it could be -1 or very occasionally MAXINT (0x) This led to strange behaviour such as: if ((1-2) == TRUE) { // works in some compilers not in others!} The correct way was to compare to FALSE or just use the result as a value: if (2-1) {//this always works} Pythons introduction of boolean type values should have gotten round all that but unfortunately it doesn't: if (1-2): print 'True' True if True: print 'True' True if (1-2) == True: print 'True' So unfortunately Pythons implementation of Boolean values is only half True(sorry couldn't resist!:-). In a true boolean implementation any non zero value should test equal to True... No language is perfect! HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with os.access function. [semantic error, if check does not work]
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing
Hi + Season's Greetings! I have put together a program that queries and modifies a Gadfly database. I have captured my output. I now want to print it to paper. I have written the output to a text file. I have searched the tutor mailing list and used the mailing list advice to get my data into nice looking columns + tables. I am using Python 2.4, Glade 2, pygtk2.8.0 + wxWidgets2.6.1. 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? Thanks, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
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
Re: [Tutor] Printing
John Corry wrote: Hi + Season's Greetings! I have put together a program that queries and modifies a Gadfly database. I have captured my output. I now want to print it to paper. I have written the output to a text file. I have searched the tutor mailing list and used the mailing list advice to get my data into nice looking columns + tables. I am using Python 2.4, Glade 2, pygtk2.8.0 + wxWidgets2.6.1. wxWidgets has support for printing, though I have never used it. See http://wxwidgets.org/manuals/2.5.3/wx_printingoverview.html#printingoverview Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor