Re: [Tutor] Raw string
instead of s='e:\mm tests\1. exp files\5.MOC-1012.exp' try to use : s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') for me here is what it gives: s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') print s e:\\mm tests\\1. exp files\\5.MOC-1012.exp s.split('') ['e:', 'mm tests', '1. exp files', '5.MOC-1012.exp'] why \\ i you only have one \ ? : you need to escape it because its a special character to the python interpreter. the r character is important in the expression s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') because it tells the interpreter to take the string as RAW and thus leave it unchanged even if it contains special characters that should actually be treated special. now to use the r or not to use it ? when to use it ? how to use it ? I always use it ! always had the result I expected, so I would suggest you use it always too specialy with the re module, ofcourse unless the result is not satisaying you, so this code (using r this time works too) : s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace(r'\\', r'') print s e:\\mm tests\\1. exp files\\5.MOC-1012.exp s.split('') ['e:', 'mm tests', '1. exp files', '5.MOC-1012.exp'] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import modeuls
If I am adding, __init__.py it still doesn't import anything. Do I have add the import (from sampletest import EmailWithoutA) in my init file? On Mon, Jul 21, 2008 at 1:28 AM, arsyed [EMAIL PROTECTED] wrote: On Sun, Jul 20, 2008 at 12:46 PM, Oleg Oltar [EMAIL PROTECTED] wrote: Hi I need to import several modules from many folders which has subfolders ~/folder/tests/sampletest.py ~/folder/suites/samplesuit.py a suit uses tests from tests folder. I need to import them somehow from tests folder. I added ~/folder to PYTHONPATH in my test_runner: import sys import os sys.path.insert(0, ~/folder) os.popen(python2.5 %s %sys.argv[1]) But when trying to import module in the samplesuite file: from tests.sampletest.EmailWithoutA import EmailWithoutA But I getting ImportError: No module named Please help Do you have an __init__.py file in the tests and suites directories? More on that here: http://docs.python.org/tut/node8.html The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.pycan just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import modeuls
On Mon, Jul 21, 2008 at 3:46 AM, Oleg Oltar [EMAIL PROTECTED] wrote: If I am adding, __init__.py it still doesn't import anything. Do I have add the import (from sampletest import EmailWithoutA) in my init file? I didn't notice this before, but I don't think python does tilde expansion in sys.path. Try using an absolute path, for example: sys.path.insert(0, ~/folder) to sys.path.insert(0, /home/user/folder') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
On Mon, Jul 21, 2008 at 9:44 AM, Monika Jisswel [EMAIL PROTECTED] wrote: instead of s='e:\mm tests\1. exp files\5.MOC-1012.exp' try to use : s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace( '\\', '') for me here is what it gives: s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') print s e:\\mm tests\\1. exp files\\5.MOC-1012.exp s.split('') ['e:', 'mm tests', '1. exp files', '5.MOC-1012.exp'] why \\ i you only have one \ ? : you need to escape it because its a special character to the python interpreter. the r character is important in the expression s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') because it tells the interpreter to take the string as RAW and thus leave it unchanged even if it contains special characters that should actually be treated special. now to use the r or not to use it ? when to use it ? how to use it ? I always use it ! always had the result I expected, so I would suggest you use it always too specialy with the re module, ofcourse unless the result is not satisaying you, so this code (using r this time works too) : s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace(r'\\', r'') print s e:\\mm tests\\1. exp files\\5.MOC-1012.exp s.split('') ['e:', 'mm tests', '1. exp files', '5.MOC-1012.exp'] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thanks, I am aware of goodies that raw string offers, but my question was how to use it with variable that already contains string. :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import modeuls
They want me to do one test runner which runs any test... And ideally it should work on any platform When I added something to $PYTHONPATH, they told me to remove it... On Mon, Jul 21, 2008 at 12:11 PM, arsyed [EMAIL PROTECTED] wrote: On Mon, Jul 21, 2008 at 4:46 AM, Oleg Oltar [EMAIL PROTECTED] wrote: Anyway. another related question. I run tests using the runner.py It has following syntax sys.path.insert(path) os.popen(python module to run) Will the python runned from the file see new path? I'm not sure but I don't think so. Try and see what happens. I think to get that behavior, you want to set the PYTHONPATH environment variable instead. Then, I believe the child process will inherit the parent process's environment variable. If that doesn't work, look into the subprocess module which takes an explicit env parameter for the Popen class in order to accomplish this. It's probably easier just to set PYTHONPATH in your shell and then run your scripts so all programs will have access to it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import modeuls
On Mon, Jul 21, 2008 at 5:15 AM, Oleg Oltar [EMAIL PROTECTED] wrote: They want me to do one test runner which runs any test... And ideally it should work on any platform When I added something to $PYTHONPATH, they told me to remove it... You can set environment variables within python, e.g.: os.environ['PYTHONPATH'] = '/some/path:' + old_path What I don't know is if child processes invoked through os.popen inherit that variable on all platforms. However, the subprocess.Popen object takes an explicit env variable for that purpose: http://docs.python.org/lib/node528.html http://docs.python.org/lib/module-subprocess.html So you should be able to use that for the same effect. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
Thanks, I am aware of goodies that raw string offers, but my question was how to use it with variable that already contains string. :) if you are reading the value from a file : import re for line in myfile: if re.search(r'e:\mm tests\1. exp files\5.MOC-1012.exp', line): line = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import modeuls
On Mon, Jul 21, 2008 at 5:15 AM, Oleg Oltar [EMAIL PROTECTED] wrote: They want me to do one test runner which runs any test... And ideally it should work on any platform You might want to look at nose or py.test, they both have test runners. I think you can give nose a directory and it will find and run the tests in the directory. http://www.somethingaboutorange.com/mrl/projects/nose/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
Neven Goršić wrote: I read from one file plenty of parameters and among them one file name of other file. That file name is 'e:\mm tests\1. exp files\5.MOC-1012.exp' and I hold it in variable s. As John pointed out, if you're really reading this string from a file (with something like file.read or ConfigParser) it should already be escaped properly, and you'd get back something like 'e:\\mm tests\\1. exp files\\5.MOC-1012.exp' instead. Which would probably work for your purposes. Can you show us an example of the approach you're using to 'read' parameters from a file, including the variable assignment? Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
I don't know in advance what the file name will be... import re for line in myfile: if re.search(r'\', line): line = line.replace('\\', '') if you have lines that contain a \ in them that you don't want to substitute then you need another if statement. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List installed python modules
Eli Brosh wrote: Hello, Is there a way to get a list of installed python modules and their versions ? i.e. I would like to know if matplotlib is installed on my computer and what version is it. And so with other modules. You've got two slightly different requirements there: 1) List all modules installed and 2) Is *this* module installed? The second is easy: code try: import ModuleX except ImportError: print ModuleX is not installed else: print ModuleX is installed print with version, getattr (ModuleX, __VERSION__, unknown) /code There's no prescribed attribute for version, so you might have to check a few like that if you didn't know what to look for. The first is harder, and in effect impossible in general. Given a well-known and controlled setup, you could do various things like interrogate the system's package database or scan sys.path looking for modules packages, but it's all a bit hazy and wouldn't take into account, say, a library module local to a particular application which differs from a centrally-installed one for version reasons. TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
2008/7/21 Martin Walsh [EMAIL PROTECTED]: Neven Goršić wrote: I read from one file plenty of parameters and among them one file name of other file. That file name is 'e:\mm tests\1. exp files\5.MOC-1012.exp' and I hold it in variable s. As John pointed out, if you're really reading this string from a file (with something like file.read or ConfigParser) it should already be escaped properly, and you'd get back something like 'e:\\mm tests\\1. exp files\\5.MOC-1012.exp' instead. Which would probably work for your purposes. Can you show us an example of the approach you're using to 'read' parameters from a file, including the variable assignment? Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I reconfigured file my parameter file. I left only one parameter in that line (path) and write path without ''. Then f.readline() get correct raw string and I can get path and file name properly by means of os.path.split(line). Thank you for your good will Neven Gorsic ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] seaxtradusa site
ok, here is the link; http://www.seaxtradusa.org/ In the footer of all webppages (not in the wiki or in the forum), is a link so i can edit pages on the site without bothering to use a shell account or upload pages or anything. I just wrote this, and it is password protected. This is a handy little feature and it is kinda useful. The link uses a server side include in a universal footer, and yes, this WILL point the script to the right page: a href=/cgi-bin/EditMyPage.py? !--#echo var=DOCUMENT_NAME --Edit This Page/a NOTE the page name is inserted into the structure with a simple SSI echo command. Place this link in the universal footer of your site, it will work. All pages so edit must be in the root directory for THIS edition, but who knows what the future holds. -- Cheers! -Kirk D Bailey THINK +-+ .*.| BOX | ..*+-+ *** THINK ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seaxtradusa site
BTW, wordwrap issue, all that is on ONE LINE. Kirk Bailey wrote: ok, here is the link; http://www.seaxtradusa.org/ In the footer of all webppages (not in the wiki or in the forum), is a link so i can edit pages on the site without bothering to use a shell account or upload pages or anything. I just wrote this, and it is password protected. This is a handy little feature and it is kinda useful. The link uses a server side include in a universal footer, and yes, this WILL point the script to the right page: a href=/cgi-bin/EditMyPage.py? !--#echo var=DOCUMENT_NAME --Edit This Page/a NOTE the page name is inserted into the structure with a simple SSI echo command. Place this link in the universal footer of your site, it will work. All pages so edit must be in the root directory for THIS edition, but who knows what the future holds. -- Cheers! -Kirk D Bailey THINK +-+ .*.| BOX | ..*+-+ *** THINK ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advice for my function, isPrime(n), please
What about the following function? if x == 0: return False return True I am a beginner, but I think it is more clear (at least to me) what the function does. And it is only one line longer than value = (x != 0) return value ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advice for my function, isPrime(n), please
On Mon, Jul 21, 2008 at 10:29 AM, Marc Tompkins [EMAIL PROTECTED] wrote: How about return (x!=0) ? Short and cryptic! Sorry - I had deleted all the old messages in the thread, and only responded to the latest. My bad - I see now that I've joined in beating a dead horse. -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advice for my function, isPrime(n), please
On Mon, Jul 21, 2008 at 10:20 AM, Daniel Sarmiento [EMAIL PROTECTED] wrote: What about the following function? if x == 0: return False return True I am a beginner, but I think it is more clear (at least to me) what the function does. And it is only one line longer than value = (x != 0) return value How about return (x!=0) ? Short and cryptic! -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter Help
Hi, I have a Python app that runs as a Windows Service. Inside this application, there is a Thread that starts a webservice. When this webservice is called, this thread displays a simple Tkinter window with some entry´s for input. But the mainloop simple stays running without presenting any popup or error. The app is running as SYSTEM on Windows. As other user, the code works great. Any advice to make this work ? Thanks in advance. Ruivaldo Neto ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
Thanks, I am aware of goodies that raw string offers, but my question was how to use it with variable that already contains string. :) If you really have to, you may use something like this: # Untested def kludge(s): s = 'r%s' % repr(s) return eval(s) Most people would frown at the use of eval (in fact I do too), because it is possible to leak dangerous code from this function, given certain string sequences. But, that's enough of a kludge and don't ask for more kludges. THE correct way to fix the problem is not here, but in the input function (the function that assigns the variable)... If you're reading from a file, and you get this problem, most probably the program that generates the file is the problem. Check the file's real content... If you're reading from standard input, make sure you use raw_input(), instead of input(), as text in input() is eval()ed first, not something we would want... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuration File Pattern
Message: 9 Date: Mon, 21 Jul 2008 16:08:45 +0200 From: Daniele [EMAIL PROTECTED] Subject: [Tutor] Configuration File Pattern To: tutor@python.org Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=UTF-8 Hi list, I've recently developed a basic python program which needs to store some data in a file (e.g. a directory path). What the program needs is a dictionary containing the data, so I used the pickle module to store the dictionary in a file and read it back when the program is launched. I wanted to know which is the common pattern in python programming in this case, because the way I've choosen only lets the user configure the data via a GUI (the file is somehow compiled). Is it better to store the data in a text file and then parse it and construct a dictionary? Or is there e third way? Following the Unix philosophy (Store data in flat text file), it is much better to dump the dictionary into a plain text file, but pickling is so damn easy. If you have the time and space to do it, you should make a dumper and parser, if not, there is no real problem with using pickled format as far as I'm aware (especially if you strictly don't want someone to manually modify the file). The only potential problem, I could think of, is whether pickled data is cross-version compatible. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Online class/education for Python?
On Sun, Jul 20, 2008 at 09:17:51PM -0400, bhaaluu wrote: If you're disciplined enough to shell out $$$ for an online class and do the work, why not just do it on your own? The tuition for a class will buy you several very nice Python books: Learning Python. Lutz. Programming Python. Lutz Core Python. Wesley Chun. Python Programming for the Absolute Beginner 2E (if you're new to programming). bhaaluu is right. There is lots of help on line. But, some prefer help and others prefer working on their own. If you do want to do it on your own, also take a look at: http://wiki.python.org/moin/BeginnersGuide http://wiki.python.org/moin/BeginnersGuide/NonProgrammers http://wiki.python.org/moin/BeginnersGuide/Programmers - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Online class/education for Python?
Hi, I have the first edition of Python Programming for the Absolute Beginner. Will this work instead of the 2nd edition? thanks, Danyelle ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding a watermark to a sequence of images
On Sun, 20 Jul 2008, Christopher Spears wrote: Has anyone used Python to watermark of sequence of images? There's a recipe for watermarking using PIL here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879 I have a half-baked program that goes through a directory of images and time-stamps them with information from the EXIF data, which I'd be happy to share with you. I started writing it because my wife likes to have our photos timestamped, and I don't, so this was our compromise; but she later came around to my point of view so I never really cleaned it up. It works, but needs some polish. It would be easy to adapt to a fixed watermark. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advice for my function, isPrime(n), please
On Mon, 21 Jul 2008, Daniel Sarmiento wrote: What about the following function? if x == 0: return False return True I don't like it, myself. You have multiple points of exit, and, yes, you can see that the fallthough is only executed if the condition is not met, but it makes you do a double-take when reading it. If you insist on having the conditional, this is clearer: if x == 0: return False else: return True I'd rather have the condition test for truth, though: if x != 0: return True else: return False But that leads to what you don't like anyway, which I think is your best solution: return x != 0 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter Help
On Mon, Jul 21, 2008 at 1:59 PM, Ruivaldo Neto [EMAIL PROTECTED] wrote: Hi, I have a Python app that runs as a Windows Service. Inside this application, there is a Thread that starts a webservice. When this webservice is called, this thread displays a simple Tkinter window with some entry´s for input. But the mainloop simple stays running without presenting any popup or error. The app is running as SYSTEM on Windows. As other user, the code works great. Any advice to make this work ? Thanks in advance. I think this is because you need to have the service marked as Interactive. This page has some information: http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx It might be worth asking the question in the py-win32 group or some windows related forum. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuration File Pattern
What I use in this situation is the INI config file parser in the standard lib. It's easy to use http://docs.python.org/lib/module-ConfigParser.html Awesome! Really easy and intuitive. Thanks a lot ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding a watermark to a sequence of images
By all means, share your script! Even if I don't use it, I can learn something from it! Thanks! --- On Mon, 7/21/08, Terry Carroll [EMAIL PROTECTED] wrote: From: Terry Carroll [EMAIL PROTECTED] Subject: Re: [Tutor] adding a watermark to a sequence of images To: [EMAIL PROTECTED], tutor@python.org Date: Monday, July 21, 2008, 1:30 PM On Sun, 20 Jul 2008, Christopher Spears wrote: Has anyone used Python to watermark of sequence of images? There's a recipe for watermarking using PIL here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879 I have a half-baked program that goes through a directory of images and time-stamps them with information from the EXIF data, which I'd be happy to share with you. I started writing it because my wife likes to have our photos timestamped, and I don't, so this was our compromise; but she later came around to my point of view so I never really cleaned it up. It works, but needs some polish. It would be easy to adapt to a fixed watermark. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter Help
Ruivaldo Neto [EMAIL PROTECTED] wrote I have a Python app that runs as a Windows Service. Inside this application, there is a Thread that starts a webservice. When this webservice is called, this thread displays a simple Tkinter window with some entry´s for input. Thats usually a very bad idea. Presenting services with GUIs can lead to lockups and other bad things under Windows so you should only do it if absolutely essential. Are you sure there is no other way to run your app? Maybe with the GUI part as a separate program that communicates with the service? But the mainloop simple stays running without presenting any popup or error. The app is running as SYSTEM on Windows. As other user, the code works great. I think the requirements of a service mean there are some extra flags that need to be set. MSDN is probably your best bet for info. It will also explain why services and GUIs don't usually mix. Any advice to make this work ? Don't do it. Find another way. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advice for my function, isPrime(n), please
At 01:42 PM 7/21/2008, Terry Carroll wrote: On Mon, 21 Jul 2008, Daniel Sarmiento wrote: What about the following function? if x == 0: return False return True I don't like it, myself. You have multiple points of exit, and, yes, you can see that the fallthough is only executed if the condition is not met, but it makes you do a double-take when reading it. If you insist on having the conditional, this is clearer: if x == 0: return False else: return True I'd rather have the condition test for truth, though: if x != 0: return True else: return False But that leads to what you don't like anyway, which I think is your best solution: return x != 0 Actually, and as I predicted, I'm beginning to like it. :-) Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Online class/education for Python?
Danyelle Gragsone [EMAIL PROTECTED] wrote I have the first edition of Python Programming for the Absolute Beginner. Will this work instead of the 2nd edition? Python changes little and slowly between versions so the answer is almost certainly yes. New features are by definition major and fast but the existing features tend to only move a little over time. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
Neven Gorsic [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Mon, Jul 21, 2008 at 9:44 AM, Monika Jisswel [EMAIL PROTECTED] wrote: instead of s='e:\mm tests\1. exp files\5.MOC-1012.exp' try to use : s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace( '\\', '') for me here is what it gives: s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') print s e:\\mm tests\\1. exp files\\5.MOC-1012.exp s.split('') ['e:', 'mm tests', '1. exp files', '5.MOC-1012.exp'] why \\ i you only have one \ ? : you need to escape it because its a special character to the python interpreter. the r character is important in the expression s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace('\\', '') because it tells the interpreter to take the string as RAW and thus leave it unchanged even if it contains special characters that should actually be treated special. now to use the r or not to use it ? when to use it ? how to use it ? I always use it ! always had the result I expected, so I would suggest you use it always too specialy with the re module, ofcourse unless the result is not satisaying you, so this code (using r this time works too) : s = r'e:\mm tests\1. exp files\5.MOC-1012.exp'.replace(r'\\', r'') print s e:\\mm tests\\1. exp files\\5.MOC-1012.exp s.split('') ['e:', 'mm tests', '1. exp files', '5.MOC-1012.exp'] Thanks, I am aware of goodies that raw string offers, but my question was how to use it with variable that already contains string. :) What it seems you don't understand is that raw strings just a method to create a string. If you already have a string read from a file, it is already created. Maybe you are confused between the representation of a string and the value of the string. s = 'c:\\abc\\123.txt' t = r'c:\abc\123.txt' s==t True Two ways to *create* the *same* string. Note there are two ways to *display* a string as well. print displays the actual value. If you don't use print, you get a representation of the string in a way that can be used to create the string in code. print s c:\abc\123.txt print t c:\abc\123.txt s 'c:\\abc\\123.txt' t 'c:\\abc\\123.txt' Note what happens if you use single backslashes without a raw to create the string: s = 'c:\abc\123.txt' s 'c:\x07bcS.txt' print s c:bcS.txt Because it wasn't a 'raw' string, the \a was interpreted as the non-printable BEL control character. \123 was interpreted as an octal constant, which turned out to be capital-S. The representation of the string contained a \x07 for the BEL control character. Since it is unprintable, the representation displayed it as a hexadecimal escape code. When you read a string from a file, the actual characters in the file end up in the string. No backslash interpretation is performed. So in your example, just read in the file and perform your operations: sample.txt contains: c:\abc\123.txt Code: import os pathname = open('sample.txt').read() pathname 'c:\\abc\\123.txt' print pathname c:\abc\123.txt print os.path.dirname(pathname) c:\abc print os.path.basename(pathname) 123.txt os.path.dirname(pathname) 'c:\\abc' os.path.basename(pathname) '123.txt' Does that clear up the confusion? --Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question about socket status
Hello, I am new to programming with python and sockets. I would like to determine the status of a socket as it is returned when you do 'netstat -a | grep port#'. I would like to know if the socket state is ESTABLISHED, LISTEN , CLOSE_WAIT, etc. Is there a way to get this information through a socket call? I've tried using socket.getperrname() function, but that only tells if there is a connection. Is there a way to get more information on the state of the socket connection? Thanks, rrupp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding a watermark to a sequence of images
On Mon, 21 Jul 2008, Christopher Spears wrote: By all means, share your script! Even if I don't use it, I can learn something from it! Well, maybe. If nothing else, perhaps you'll learn some new bad habits. The timestamp on this file shows that I dropped this project in June 2005, and you can see it's unfinished. It still has a few raw edges (including a prompt for a preserve EXIF feature that never got written; and lots of debugging print statements), but it's not bad as a proof-of-concept. The code imports and depends on Gene Cash's EXIF.py module; it now apparently lives at http://sourceforge.net/projects/exif-py/ rather than the URL in the code. But still, if the EXIF module doesn't exist, it just uses the file's timestamp as the date -- not exactly the safest way of figuring the date the photo was taken. At the time I wrote this, I was unaware that PIL had some then-undocumented EXIF support; I probably would have used that to avoid importing a non-standard module. Use this code however you wish, if you find it helpful. Ignore the copyright statement; BaishuSoft was just a little joke between my wife and me. --snip--- import Tkinter as Tk import tkFileDialog import os, sys, time import Image, ImageDraw, ImageFont, ImageTk class MyApp: Begin a Tkinter-based application def __init__(self, root): initializer for Tkinter-based application self.root=root self.root.title(Picture TimeStamper) NoticeFrame = Tk.Frame(self.root) NoticeFrame.pack() headertext = u Baishusoft Picture TimeStamper \U00A9 2005 Baishusoft LLP Tk.Label(NoticeFrame,text=headertext).pack() ButtonFrame = Tk.Frame(self.root) ButtonFrame.pack() SelButton = Tk.Button(ButtonFrame, text=Select Directory, command=self.select) SelButton.pack(side=left) QuitButton = Tk.Button(ButtonFrame, text=Quit, command=self.quit) QuitButton.pack(side=left) OptionsFrame = Tk.Frame(self.root) OptionsFrame.pack() self.EXIFvar = Tk.IntVar() self.EXIFCheckbox = Tk.Checkbutton( OptionsFrame, text=Preserve EXIF (requires JHead), variable = self.EXIFvar) self.EXIFCheckbox.pack(side=top) self.Progressvar = Tk.IntVar() self.ProgressCheckbox = Tk.Checkbutton( OptionsFrame, text=Show progress, variable = self.Progressvar) self.ProgressCheckbox.pack(side=left) self.StatusFrame = Tk.Frame(self.root) self.StatusFrame.pack() self.ImageLabel = Tk.Label(self.StatusFrame) self.ImageLabel.pack() self.FilenameLabel = Tk.Label(self.StatusFrame) self.FilenameLabel.pack() def select(self): dirname = tkFileDialog.askdirectory() if dirname != '': os.path.walk(dirname, self.process_files, None) print PROCESSING COMPLETED. SELECT MORE FILES OR QUIT. else: print NO DIRECTORY SELECTED. return def quit(self): print EXITING. sys.exit() def process_files(self, junk, dirpath, namelist): for filename in namelist: stamped_filename = self.getstampedfilename(filename) if stamped_filename is not None: if os.path.isfile(os.path.join(dirpath,stamped_filename)): print FILE EXISTS, SKIPPING:, stamped_filename else: self.updatestatus(dirpath, filename) datetext = self.timestamp(dirpath, filename, stamped_filename) print FILE IMPRINTED:, stamped_filename, datetext def updatestatus(self,dirpath,filename): im=Image.open(os.path.join(dirpath, filename)) print time.asctime(), thumbnailing..., filename, im.mode, im.size im.thumbnail((100,75)) print time.asctime(), showing..., filename, im.mode, im.size #im.show() self.Tkimage = ImageTk.PhotoImage(im) print created self.ImageLabel.config(image=self.Tkimage) self.ImageLabel.pack() self.FilenameLabel.config(text=filename) self.FilenameLabel.pack() self.StatusFrame.pack() self.root.update() def getstampedfilename(self, filename): fn, ft = os.path.splitext(filename) if ft.upper() in [.JPG, .JPEG] and \ not (fn.upper().endswith(-DATED)): return fn + -dated + ft else: return None def timestamp(self, dirpath, original_filename, new_filename): full_original_filename = os.path.join(dirpath, original_filename) full_new_filename = os.path.join(dirpath, new_filename) font=ImageFont.truetype(Arial.ttf, 40) datetext = GetFileDate(full_original_filename)