On Thu, 12 May 2005 15:41:14 +0200, George <[EMAIL PROTECTED]> wrote:
>Newbie question: > >I'm trying to lauch Notepad from Python to open a textfile: > >import os >b1="c:\test.txt" >os.system('notepad.exe ' + b1) > >However, the t of test is escaped by the \, resulting in Notepad trying >to open "c: est.txt". Right. '\t' is a tab, not two characters. You can get the characters you want by escaping the escape > >How do I solve this? > >(By the way, b1 comes from a command line parameter, so the user enters >c:\test.txt as command line parameter.) It should be ok then, unless you have somehow processed the command line parameter and interpreted the backslash as an escape. E.g., pargs.py here prints command line args and backslash is an ordinary string character as you see in argv[3] below. If it were a tab, you would see whitespace instead of the backslash. [ 7:35] C:\pywk\parse\ast>type c:\util\pargs.py import sys for i in xrange(len(sys.argv)): print 'argv[%d]: "%s"' % (i,sys.argv[i]) [ 7:35] C:\pywk\parse\ast>py24 c:\util\pargs.py abc def c:\test.txt argv[0]: "c:\util\pargs.py" argv[1]: "abc" argv[2]: "def" argv[3]: "c:\test.txt" If by "command line" you mean your own programmed input, make sure you use raw_input, not input, e.g., >>> print '-->>%s<<--' % raw_input('Enter text please: > ') Enter text please: > c:\test.txt -->>c:\test.txt<<-- But input evaluates the input string (also posing security problems if you don't trust the user): >>> print '-->>%s<<--' % input('Enter text please: > ') Enter text please: > c:\test.txt Traceback (most recent call last): File "<stdin>", line 1, in ? File "<string>", line 1 c:\test.txt ^ SyntaxError: invalid syntax That was from evaluating, so we can quote: >>> print '-->>%s<<--' % input('Enter text please: > ') Enter text please: > "c:\test.txt" -->>c: est.txt<<-- ^^--there's that tab \t again, unless you escape the backslash >>> print '-->>%s<<--' % input('Enter text please: > ') Enter text please: > "c:\\test.txt" -->>c:\test.txt<<-- But in your example above, >>> b1="c:\test.txt" >>> b1 'c:\test.txt' >>> list(b1) ['c', ':', '\t', 'e', 's', 't', '.', 't', 'x', 't'] >>> print '-->>%s<<--'%b1 -->>c: est.txt<<-- Escaping the escape: >>> b1="c:\\test.txt" >>> print '-->>%s<<--'%b1 -->>c:\test.txt<<-- Using raw string format (prefixed r on string), which won't work if string ends in backslash BTW) >>> b1=r"c:\test.txt" >>> print '-->>%s<<--'%b1 -->>c:\test.txt<<-- To see the single tab character in your original >>> b1="c:\test.txt" >>> b1[2] '\t' >>> ord(b1[2]) 9 BTW, you might want to use os.system('start notepad ' + b1) if you want an independent process and not wait for notepad to finish. Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list