Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Launch file in Notepad

Reply
Thread Tools

Launch file in Notepad

 
 
George
Guest
Posts: n/a
 
      05-12-2005
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".

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.)

George
 
Reply With Quote
 
 
 
 
Brian van den Broek
Guest
Posts: n/a
 
      05-12-2005
George said unto the world upon 2005-05-12 09:41:
> 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".
>
> How do I solve this?


There are several ways, but the preferred solution is to switch the
slash direction: "c:/test.txt". Python's smart enough to notice its
running on Windows and do the right thing with the slash. (Other
choice include using raw strings: r"c:\test.txt", and explicitly
escaping the backslash: "c:\\test.txt".)

Best,

Brian vdB

 
Reply With Quote
 
 
 
 
Richie Hindle
Guest
Posts: n/a
 
      05-12-2005

[George]
> b1="c:\test.txt"


With this code, your problem is the embedded tab as you say. Use either
r"c:\test.txt" or "c:\\test.txt". However, if this is true:

> By the way, b1 comes from a command line parameter, so the user enters
> c:\test.txt as command line parameter.


then there will be no embedded tab. How are you prompting the user? When
I run this:

import os
b1=raw_input("Enter a filename: ")
os.system('notepad.exe ' + b1)

and enter c:\test.txt, it works as expected.

[Brian]
> There are several ways, but the preferred solution is to switch the
> slash direction: "c:/test.txt". Python's smart enough to notice its
> running on Windows and do the right thing with the slash.


In fairness to Windows, I don't believe Python does anything special here.
Windows itself happily accepts either forward slash or backslash at the OS
level - it's only the shells (explorer.exe or to a lesser extent cmd.exe)
that don't accept forward slashes.

--
Richie Hindle
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      05-12-2005
On 2005-05-12, Brian van den Broek <(E-Mail Removed)> wrote:

>> 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".


> There are several ways, but the preferred solution is to switch the
> slash direction: "c:/test.txt". Python's smart enough to notice its
> running on Windows and do the right thing with the slash.


Does Python really look at the string and mess with the slash?
I don't think it needs to, since the Windows system calls have
always accepted forward slashses, haven't they?

--
Grant Edwards grante Yow! Ask me the DIFFERENCE
at between PHIL SILVERS and
visi.com ALEXANDER HAIG!!
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      05-12-2005
Richie Hindle wrote:
>>By the way, b1 comes from a command line parameter, so the user enters
>>c:\test.txt as command line parameter.

>
>
> How are you prompting the user? When I run this:
>
> import os
> b1=raw_input("Enter a filename: ")
> os.system('notepad.exe ' + b1)
>
> and enter c:\test.txt, it works as expected.


So it does with me, but the user does not enter the filename, he
launches my Python script file from the DOS-prompt with a command line
parameter, for example:

test.py c:\test.txt

In that case the \ escapes the t. (read on!)

That is, until now. For some very strange reason it suddenly works as
expected. I don't understand it anymore, but never mind. Maybe I changed
my little proggie somehow, causing it accidentally to work. Thanks anyway.

George
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      05-12-2005
On Thu, 12 May 2005 15:41:14 +0200, George <gtog@_no___spam_myrealbox.com> 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
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      05-12-2005
Grant Edwards wrote:
> On 2005-05-12, Brian van den Broek <(E-Mail Removed)> wrote:


> Does Python really look at the string and mess with the slash?
> I don't think it needs to, since the Windows system calls have
> always accepted forward slashses, haven't they?


It did, but now not anymore. I don't understand why, maybe I've changed
something in the code. See my other post.

George
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      05-12-2005
Bengt Richter wrote:
> On Thu, 12 May 2005 15:41:14 +0200, George <gtog@_no___spam_myrealbox.com> wrote:


>>(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.


Perhaps that's what I did (processing the command line parameter). For
some reason it works now.

> If by "command line" you mean your own programmed input, make sure you use raw_input, not input, e.g.,


I was referring to the user launching my script with a filename as
parameter:

test.py c:\test.txt

Here's my code so far (it removes blank lines from the input file (for
example c:\test.txt), and creates a new file (c:\test_new.txt) to store
the output):

import string
import sys
import os
if len(sys.argv)<=1:
print 'Usage: dbl.py [filename]'
sys.exit()
b1=sys.argv[1]
b2=b1[:-4] + '_new' + b1[-4:]
f1=open(b1,'r')
f2=open(b2,'w')
r1=f1.readlines()
for r in r1:
if string.capwords(r)<>'':
f2.write(r)
f1.close()
f2.close()
print 'Output file: ' + b2
os.system ('start notepad.exe ' + b2)

George
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      05-12-2005
On Thu, 12 May 2005 14:20:29 -0000, Grant Edwards <(E-Mail Removed)> wrote:

>On 2005-05-12, Brian van den Broek <(E-Mail Removed)> wrote:
>
>>> 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".

>
>> There are several ways, but the preferred solution is to switch the
>> slash direction: "c:/test.txt". Python's smart enough to notice its
>> running on Windows and do the right thing with the slash.

>
>Does Python really look at the string and mess with the slash?
>I don't think it needs to, since the Windows system calls have
>always accepted forward slashses, haven't they?
>

For a path parameter, I think so. But various command shells us '/' the
way unix uses '-' -- i.e., for options/switches. E.g.
ls -R foo/bar
would work as
dir /s "foo/bar"
since the shell would pass on the quoted string to the os level (with quotes removed)
Likewise
dir foo\bar/s
would work, but not
dir foo/bar/s
or
dir/s foo/bar

I don't know why MS used backslashes when unix had a perfectly good path syntax
(not to mention drive letter idiocy). Maybe some legal idiocy, wanting to be
different to be safe from SCO types?

Regards,
Bengt Richter
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      05-12-2005
On 2005-05-12, Bengt Richter <(E-Mail Removed)> wrote:

>>> There are several ways, but the preferred solution is to switch the
>>> slash direction: "c:/test.txt". Python's smart enough to notice its
>>> running on Windows and do the right thing with the slash.

>>
>>Does Python really look at the string and mess with the slash?
>>I don't think it needs to, since the Windows system calls have
>>always accepted forward slashses, haven't they?

>
> For a path parameter, I think so. But various command shells us '/' the
> way unix uses '-' -- i.e., for options/switches. E.g.


You're right. It's the applications that choke on the forward
slash. Back in the bad old days there was a way to tell DOS to
use '-' for the switch character, but not all apps paid
attention to the setting.

> I don't know why MS used backslashes when unix had a perfectly
> good path syntax (not to mention drive letter idiocy). Maybe
> some legal idiocy, wanting to be different to be safe from SCO
> types?


I think the use of forward slashes for command line switches
was adopted by CP/M from DEC's OSes (e.g. RSX-11). CP/M didn't
have directories in the beginning, so nobody worried about what
to use for path separators (DEC used colons, IIRC). DOS was a
straight-up ripoff of CP/M and kept '/' as the option
character. Later when directories were added to DOS, they
picked '\' as the path seperator becuase '/' was the default
switch caracter. The C-language "system calls" always accepted
either, but they may have been translating forward to backward
before making the DOS call calls.

--
Grant Edwards grante Yow! .. If I had heart
at failure right now,
visi.com I couldn't be a more
fortunate man!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot launch simple applications (notepad, java) using Apache and PERL(CGI) JDS Perl 4 11-11-2008 08:50 PM
How to open text file (.log, .txt etc) in notepad from aspnet =?Utf-8?B?Uk4gRGFz?= ASP .Net 5 05-23-2008 06:53 AM
how to use io operations with open/save file dialog box in file menu for simple notepad boris Java 1 07-22-2007 04:13 PM
RE: Launch file in Notepad Michael.Coll-Barth@VerizonWireless.com Python 0 05-12-2005 01:52 PM
Cannot launch simple applications (notepad, java) using Apache and PERL(CGI) JDS Perl Misc 3 05-24-2004 06:33 PM



Advertisments