473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Launch file in Notepad

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
Jul 19 '05 #1
17 20999
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

Jul 19 '05 #2

[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
ri****@entrian.com

Jul 19 '05 #3
On 2005-05-12, Brian van den Broek <bv****@po-box.mcgill.ca> 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!!
Jul 19 '05 #4
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
Jul 19 '05 #5
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
Jul 19 '05 #6
Grant Edwards wrote:
On 2005-05-12, Brian van den Broek <bv****@po-box.mcgill.ca> 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
Jul 19 '05 #7
On Thu, 12 May 2005 14:20:29 -0000, Grant Edwards <gr****@visi.com> wrote:
On 2005-05-12, Brian van den Broek <bv****@po-box.mcgill.ca> 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
Jul 19 '05 #8
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
Jul 19 '05 #9
On 2005-05-12, Bengt Richter <bo**@oz.net> 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!!
Jul 19 '05 #10
On Thu, 12 May 2005 15:14:09 GMT, bo**@oz.net (Bengt Richter) declaimed
the following in comp.lang.python:


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?
The MS-DOS precursor (and/or CP/M) used "/" to introduce command
line options. This meant, when subdirectories were introduced, they had
to come up with a new separator character.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #11
Brian van den Broek wrote:
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.


that's slightly misleading: on the API level, most functions can handle
both kinds of slashes. functions like open(), os.remove(), shutil.copy()
etc. handles either case just fine. and in most cases, this is handled
on the Win API level (or in the C library), not by Python.

however, in this case, the user passes a string to os.system(). that
string is passed *as is* to the command shell (which, in this case,
passes it on to notepad.exe as is).

</F>

Jul 19 '05 #12
On Thu, 12 May 2005 16:30:36 GMT, Dennis Lee Bieber <wl*****@ix.netcom.com> wrote:
On Thu, 12 May 2005 15:14:09 GMT, bo**@oz.net (Bengt Richter) declaimed
the following in comp.lang.python:


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?

The MS-DOS precursor (and/or CP/M) used "/" to introduce command
line options. This meant, when subdirectories were introduced, they had
to come up with a new separator character.

Or overcome some NIH and switch to '-' for command line options? ;-)

Regards,
Bengt Richter
Jul 19 '05 #13
George 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".

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


The \t will only be interpreted as TAB if it was entered as part of
your python code. If the \t was entered as a command line arg it will
be interpreted as a \ and a t. For example:
#test.py
import sys
b1 = sys.argv[1]
b2 = "C:\test.txt"
print b1
print b2

will result in this:

C:\>test.py C:\test.txt
C:\test.txt
C: est.txt
-greg
Jul 19 '05 #14
Fredrik Lundh said unto the world upon 2005-05-12 13:52:
Brian van den Broek wrote:

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.

that's slightly misleading: on the API level, most functions can handle
both kinds of slashes. functions like open(), os.remove(), shutil.copy()
etc. handles either case just fine. and in most cases, this is handled
on the Win API level (or in the C library), not by Python.

however, in this case, the user passes a string to os.system(). that
string is passed *as is* to the command shell (which, in this case,
passes it on to notepad.exe as is).

</F>


Thanks to Fredrik and everyone else who contributed to the thread to
correct my mistake.

Best to all,

Brian vdB

Jul 19 '05 #15
On Thu, 12 May 2005 15:34:39 -0000, Grant Edwards <gr****@visi.com>
declaimed the following in comp.lang.python:

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
I can't speak for the various PDP-11 family, but VMS syntax was:

hardware:[toplevel.nextlevel.ad.infinitum]filename.ext;version

where hardware could be a drive specification or a logical name (only
other OS I've encountered with logical names is AmigaOS, which also
allowed one to specify disks by volume label -- and would prompt the
user to insert volume X into a drive if needed!)

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #16
On 2005-05-13, Dennis Lee Bieber <wl*****@ix.netcom.com> wrote:
On Thu, 12 May 2005 15:34:39 -0000, Grant Edwards <gr****@visi.com>
declaimed the following in comp.lang.python:
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
I can't speak for the various PDP-11 family, but VMS syntax was:

hardware:[toplevel.nextlevel.ad.infinitum]filename.ext;version


Ah yes, that's looking familiar. The colon separated the
"logical drive" from the path in brackets, in which the
seperator was a dot. I spent most of my time under VMS running
DECShell, so I used mostly 'normal' Unix path syntax.
where hardware could be a drive specification or a logical name (only
other OS I've encountered with logical names is AmigaOS, which also
allowed one to specify disks by volume label -- and would prompt the
user to insert volume X into a drive if needed!)


--
Grant Edwards grante Yow! Don't hit me!! I'm in
at the Twilight Zone!!!
visi.com
Jul 19 '05 #17

"Dennis Lee Bieber" <wl*****@ix.netcom.com> wrote in message
news:n2********************************@4ax.com...
On Thu, 12 May 2005 15:34:39 -0000, Grant Edwards <gr****@visi.com>
declaimed the following in comp.lang.python:

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


I can't speak for the various PDP-11 family, but VMS syntax was:

hardware:[toplevel.nextlevel.ad.infinitum]filename.ext;version


And in a clustered VMS environment, there's also
nodename::hardware:[toplevel.nextlevel.ad.infinitum]filename.ext;version
(somewhat analogous to a UNC path)

Roger

--
This signature was intentionally left blank.
(except it's not, wtf ??????)

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 19 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Milon | last post by:
I have 2 computers hooked to the same network. One of the computer has Python installed, and I want to write a python script to launch "c:\notepad.exe" on the second computer, how do I do this? ...
9
by: Sandy | last post by:
can mfc application, send text data to opened notepad file in desktop?(live transfer of data) . can anybody help
24
by: Kelly | last post by:
Hey all - I need a little more help. I don't quite know why my text file or form isn't closing. Short version - this program takes data entered into a textbox, user clicks Save button, Save...
3
by: Peter Rilling | last post by:
When I installed VS.NET, I noticed that the solution files (having the extension .sln) seem to know what version of the ide created it. If I have previous version, the icon changes and when...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
6
by: saranraj | last post by:
Platform : Visual Studio 2005 Language : Visual C# We need code to retrieve data from notepad in client side for WebApplication
7
by: jocago | last post by:
Good afternoon from someone who is trying to learn Python. I would like to launch an app from within a Python script. From the examples I have found, I should be able to do this with os.system. ...
0
by: Matthew Barnett | last post by:
Steve Holden wrote: 20 Some text editors can display very long lines, others can't and just wrap them when displaying. Notepad can't.
25
by: stefbek97 | last post by:
Hello All, I am having a bit of trouble opening a file in my ASP.Net/C# Application. I have a file Path and name stored in my Database. I display that filename in a Gridview control. I also have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.