473,732 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: problems with opening files due to file's path


Gerhard Häring wrote:

Alexnb wrote:
>Okay, so what I want my program to do it open a file, a music file in
specific, and for this we will say it is an .mp3. Well, I am using the
system() command from the os class. [...]

system("\"C:\D ocuments and Settings\Alex\M y Documents\My
Music\Rhapsody \Bryanbros\Weez er\(2001)\04 - Island In The Sun.wma\"")
[...]
Try os.startfile() instead. It should work better.

-- Gerhard

--
http://mail.python.org/mailman/listinfo/python-list

No, it didn't work, but it gave me some interesting feedback when I ran it
in the shell. Heres what it told me:
>>os.startfile( "C:\Documen ts and Settings\Alex\M y Documents\My
Music\Rhapsod y\Bryanbros\Jas on Mraz\I'm Yours (Single)\01 - I'm
Yours.wma")
Traceback (most recent call last):
File "<pyshell#1 0>", line 1, in <module>
os.startfile("C :\Documents and Settings\Alex\M y Documents\My
Music\Rhapsody\ Bryanbros\Jason Mraz\I'm Yours (Single)\01 - I'm Yours.wma")

WindowsError: [Error 2] The system cannot find the file specified:
"C:\\Docume nts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsody \\Bryanbros\\Ja son Mraz\\I'm Yours (Single)\x01 - I'm
Yours.wma"

See it made each backslash into two, and the one by the parenthesis and the
0 turned into an x....
--
View this message in context: http://www.nabble.com/problems-with-...p17759825.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
34 5360
On Jun 10, 11:45*am, Alexnb <alexnbr...@gma il.comwrote:
Gerhard Häring wrote:
Alexnb wrote:
Okay, so what I want my program to do it open a file, a music file in
specific, and for this we will say it is an .mp3. Well, I am using the
system() command from the os class. [...]
system("\"C:\Do cuments and Settings\Alex\M y Documents\My
Music\Rhapsody\ Bryanbros\Weeze r\(2001)\04 - Island In The Sun.wma\"")
[...]
Try os.startfile() instead. It should work better.
-- Gerhard
--
http://mail.python.org/mailman/listinfo/python-list

No, it didn't work, but it gave me some interesting feedback when I ran it
in the shell. Heres what it told me:
>os.startfile(" C:\Documents and Settings\Alex\M y Documents\My
Music\Rhapsody \Bryanbros\Jaso n Mraz\I'm Yours (Single)\01 - I'm
Yours.wma")

Traceback (most recent call last):
* File "<pyshell#1 0>", line 1, in <module>
* * os.startfile("C :\Documents and Settings\Alex\M y Documents\My
Music\Rhapsody\ Bryanbros\Jason Mraz\I'm Yours (Single)\01 - I'm Yours.wma")

WindowsError: [Error 2] The system cannot find the file specified:
"C:\\Docume nts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsody \\Bryanbros\\Ja son Mraz\\I'm Yours (Single)\x01 - I'm
Yours.wma"

See it made each backslash into two, and the one by the parenthesis and the
0 turned into an x....
--
View this message in context:http://www.nabble.com/problems-with-...o-file%27s-pat...
Sent from the Python - python-list mailing list archive at Nabble.com.
Yeah. You need to either double all the backslashes or make it a raw
string by adding an "r" to the beginning, like so:

os.startfile(r' C:\path\to\my\f ile')

HTH

Mike
Jun 27 '08 #2

Hey thanks!, both the raw and the double backslashes worked. You are a
gentleman and a scholar.

Mike Driscoll wrote:

On Jun 10, 11:45 am, Alexnb <alexnbr...@gma il.comwrote:
>Gerhard Häring wrote:
Alexnb wrote:
Okay, so what I want my program to do it open a file, a music file in
specific, and for this we will say it is an .mp3. Well, I am using the
system() command from the os class. [...]
>system("\"C:\D ocuments and Settings\Alex\M y Documents\My
Music\Rhapsody \Bryanbros\Weez er\(2001)\04 - Island In The Sun.wma\"")
[...]
Try os.startfile() instead. It should work better.
-- Gerhard
--
http://mail.python.org/mailman/listinfo/python-list

No, it didn't work, but it gave me some interesting feedback when I ran
it
in the shell. Heres what it told me:
>>os.startfile( "C:\Documen ts and Settings\Alex\M y Documents\My
Music\Rhapsod y\Bryanbros\Jas on Mraz\I'm Yours (Single)\01 - I'm
Yours.wma")

Traceback (most recent call last):
File "<pyshell#1 0>", line 1, in <module>
os.startfile("C :\Documents and Settings\Alex\M y Documents\My
Music\Rhapsody \Bryanbros\Jaso n Mraz\I'm Yours (Single)\01 - I'm
Yours.wma")

WindowsError : [Error 2] The system cannot find the file specified:
"C:\\Documen ts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsod y\\Bryanbros\\J ason Mraz\\I'm Yours (Single)\x01 - I'm
Yours.wma"

See it made each backslash into two, and the one by the parenthesis and
the
0 turned into an x....
--
View this message in
context:http://www.nabble.com/problems-with-...o-file%27s-pat...
Sent from the Python - python-list mailing list archive at Nabble.com.
Yeah. You need to either double all the backslashes or make it a raw
string by adding an "r" to the beginning, like so:

os.startfile(r' C:\path\to\my\f ile')

HTH

Mike
--
http://mail.python.org/mailman/listinfo/python-list
--
View this message in context: http://www.nabble.com/problems-with-...p17761126.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #3

Well, now i've hit another problem, this time being that the path will be a
variable, and I can't figure out how to make startfile() make it raw with a
variable, if I put startfile(r variable), it doesn't work and
startfile(rvari able) obviously won't work, do you know how to make that work
or better yet, how to take a regular string that is given and make every
single "\" into a double "\\"?

Mike Driscoll wrote:

On Jun 10, 11:45 am, Alexnb <alexnbr...@gma il.comwrote:
>Gerhard Häring wrote:
Alexnb wrote:
Okay, so what I want my program to do it open a file, a music file in
specific, and for this we will say it is an .mp3. Well, I am using the
system() command from the os class. [...]
>system("\"C:\D ocuments and Settings\Alex\M y Documents\My
Music\Rhapsody \Bryanbros\Weez er\(2001)\04 - Island In The Sun.wma\"")
[...]
Try os.startfile() instead. It should work better.
-- Gerhard
--
http://mail.python.org/mailman/listinfo/python-list

No, it didn't work, but it gave me some interesting feedback when I ran
it
in the shell. Heres what it told me:
>>os.startfile( "C:\Documen ts and Settings\Alex\M y Documents\My
Music\Rhapsod y\Bryanbros\Jas on Mraz\I'm Yours (Single)\01 - I'm
Yours.wma")

Traceback (most recent call last):
File "<pyshell#1 0>", line 1, in <module>
os.startfile("C :\Documents and Settings\Alex\M y Documents\My
Music\Rhapsody \Bryanbros\Jaso n Mraz\I'm Yours (Single)\01 - I'm
Yours.wma")

WindowsError : [Error 2] The system cannot find the file specified:
"C:\\Documen ts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsod y\\Bryanbros\\J ason Mraz\\I'm Yours (Single)\x01 - I'm
Yours.wma"

See it made each backslash into two, and the one by the parenthesis and
the
0 turned into an x....
--
View this message in
context:http://www.nabble.com/problems-with-...o-file%27s-pat...
Sent from the Python - python-list mailing list archive at Nabble.com.
Yeah. You need to either double all the backslashes or make it a raw
string by adding an "r" to the beginning, like so:

os.startfile(r' C:\path\to\my\f ile')

HTH

Mike
--
http://mail.python.org/mailman/listinfo/python-list
--
View this message in context: http://www.nabble.com/problems-with-...p17761338.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #4
On Jun 10, 1:25*pm, "Thomas Morton" <morton.tho...@ googlemail.com>
wrote:
maybe try string substitution... not sure if that's really the BEST way to
do it but it should work

startfile(r"%s" %variable)

I concur. That should work. A slightly more in depth example (assuming
Windows):

os.startfile(r' C:\Documents and Settings\%s\Des ktop\myApp.exe' %
username)

or

os.startfile(r' C:\Program Files\%s' % myApp)

Hopefully this is what you are talking about. If you were referring to
passing in arguments, than you'll want to use the subprocess module
instead.

>
--------------------------------------------------
From: "Alexnb" <alexnbr...@gma il.com>
Sent: Tuesday, June 10, 2008 7:05 PM
To: <python-l...@python.org >
Subject: Re: problems with opening files due to file's path
Well, now i've hit another problem, this time being that the path will be
a
variable, and I can't figure out how to make startfile() make it raw with
a
variable, if I put startfile(r variable), it doesn't work and
startfile(rvari able) obviously won't work, do you know how to make that
work
or better yet, how to take a regular string that is given and make every
single "\" into a double "\\"?
<snip>

Mike
Jun 27 '08 #5

That would work, but not for what I want. See the file could be anywhere on
the user's system and so the entire path will be unique, and that didn't
work with a unique path. What is the subprocess module you are talking
about?

Mike Driscoll wrote:

On Jun 10, 1:25Â*pm, "Thomas Morton" <morton.tho...@ googlemail.com>
wrote:
>maybe try string substitution... not sure if that's really the BEST way
to
do it but it should work

startfile(r"%s "%variable)

I concur. That should work. A slightly more in depth example (assuming
Windows):

os.startfile(r' C:\Documents and Settings\%s\Des ktop\myApp.exe' %
username)

or

os.startfile(r' C:\Program Files\%s' % myApp)

Hopefully this is what you are talking about. If you were referring to
passing in arguments, than you'll want to use the subprocess module
instead.

>>
--------------------------------------------------
From: "Alexnb" <alexnbr...@gma il.com>
Sent: Tuesday, June 10, 2008 7:05 PM
To: <python-l...@python.org >
Subject: Re: problems with opening files due to file's path
Well, now i've hit another problem, this time being that the path will
be
a
variable, and I can't figure out how to make startfile() make it raw
with
a
variable, if I put startfile(r variable), it doesn't work and
startfile(rvari able) obviously won't work, do you know how to make that
work
or better yet, how to take a regular string that is given and make
every
single "\" into a double "\\"?
<snip>

Mike
--
http://mail.python.org/mailman/listinfo/python-list
--
View this message in context: http://www.nabble.com/problems-with-...p17762276.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #6
heh thanks Mike - glad im not going mad :P

Just tested locally in IDLE (I know I know!) and it works for me like this:
>>test = os.path.join(os .getcwd(),"NEWS .txt")
test
'D:\\Python25\\ NEWS.txt'
>>os.startfile( r"%s"%test)
And the file opens...

Does the file definitely exist?

Tom
--------------------------------------------------
From: "Alexnb" <al********@gma il.com>
Sent: Tuesday, June 10, 2008 7:37 PM
To: <py*********@py thon.org>
Subject: Re: problems with opening files due to file's path
>
No this time it perhaps gave me the worst of all heres what I entered, and
the output
>>>startfile(r" %s"%full) ***full is the path***

startfile(r"%s" %full)

WindowsError: [Error 2] The system cannot find the file specified:
'"C:\\Docume nts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsody \\Bryanbros\\Ja son Mraz\\I\'m Yours (Single)\x01 - I\'m
Yours.wma"'
Thomas Morton wrote:
>>
maybe try string substitution... not sure if that's really the BEST way
to
do it but it should work

startfile(r"%s "%variable)

--------------------------------------------------
From: "Alexnb" <al********@gma il.com>
Sent: Tuesday, June 10, 2008 7:05 PM
To: <py*********@py thon.org>
Subject: Re: problems with opening files due to file's path
>>>
Well, now i've hit another problem, this time being that the path will
be
a
variable, and I can't figure out how to make startfile() make it raw
with
a
variable, if I put startfile(r variable), it doesn't work and
startfile(rva riable) obviously won't work, do you know how to make that
work
or better yet, how to take a regular string that is given and make every
single "\" into a double "\\"?

Mike Driscoll wrote:

On Jun 10, 11:45 am, Alexnb <alexnbr...@gma il.comwrote:
Gerhard Häring wrote:
>
Alexnb wrote:
Okay, so what I want my program to do it open a file, a music file
in
specific, and for this we will say it is an .mp3. Well, I am using
the
system() command from the os class. [...]
>
system("\"C :\Documents and Settings\Alex\M y Documents\My
Music\Rhaps ody\Bryanbros\W eezer\(2001)\04 - Island In The
Sun.wma\" ")
[...]
>
Try os.startfile() instead. It should work better.
>
-- Gerhard
>
--
http://mail.python.org/mailman/listinfo/python-list
>
No, it didn't work, but it gave me some interesting feedback when I
ran
it
in the shell. Heres what it told me:
>
>os.startfi le("C:\Document s and Settings\Alex\M y Documents\My
>Music\Rhap sody\Bryanbros\ Jason Mraz\I'm Yours (Single)\01 - I'm
>Yours.wma" )
>
Traceback (most recent call last):
File "<pyshell#1 0>", line 1, in <module>
os.startfile("C :\Documents and Settings\Alex\M y Documents\My
Music\Rhaps ody\Bryanbros\J ason Mraz\I'm Yours (Single)\01 - I'm
Yours.wma ")
>
WindowsErro r: [Error 2] The system cannot find the file specified:
"C:\\Docume nts and Settings\\Alex\ \My Documents\\My
Music\\Rhap sody\\Bryanbros \\Jason Mraz\\I'm Yours (Single)\x01 - I'm
Yours.wma "
>
See it made each backslash into two, and the one by the parenthesis
and
the
0 turned into an x....
--
View this message in
context:http://www.nabble.com/problems-with-...o-file%27s-pat...
Sent from the Python - python-list mailing list archive at Nabble.com.

Yeah. You need to either double all the backslashes or make it a raw
string by adding an "r" to the beginning, like so:

os.startfile (r'C:\path\to\m y\file')

HTH

Mike
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context:
http://www.nabble.com/problems-with-...p17761338.html
Sent from the Python - python-list mailing list archive at Nabble.com.

--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context:
http://www.nabble.com/problems-with-...p17761946.html
Sent from the Python - python-list mailing list archive at Nabble.com.

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #7
Alexnb wrote:
No this time it perhaps gave me the worst of all heres what I entered, and
the output
>>>startfile(r" %s"%full) ***full is the path***

startfile(r"%s" %full)

WindowsError: [Error 2] The system cannot find the file specified:
'"C:\\Docume nts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsody \\Bryanbros\\Ja son Mraz\\I\'m Yours (Single)\x01 - I\'m
Yours.wma"'
Contrary to what other posters have asserted, doing the above can't make
a difference. Putting 'r' in front of a string literal tells Python not
to give backslashes in the string literal any special treatment. Since
there are no backslashes in "%s", the 'r' does nothing.

Therefore, (r"%s"%full) is the same as ("%s"%full), which is the same as
(full), assuming that `full` is the name of a string.

The real answer lies in fixing the code where you're assigning the
pathname to 'full', which you haven't posted. Please post the code where
you're assigning the pathname, or better yet, post the complete code
you're running.

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 27 '08 #8
On Jun 10, 2:09*pm, Carsten Haese <carsten.ha...@ gmail.comwrote:
Alexnb wrote:
No this time it perhaps gave me the worst of all heres what I entered, and
the output
>>startfile(r"% s"%full) * ****full is the path***
startfile(r"%s" %full)
WindowsError: [Error 2] The system cannot find the file specified:
'"C:\\Docume nts and Settings\\Alex\ \My Documents\\My
Music\\Rhapsody \\Bryanbros\\Ja son Mraz\\I\'m Yours (Single)\x01 - I\'m
Yours.wma"'

Contrary to what other posters have asserted, doing the above can't make
a difference. Putting 'r' in front of a string literal tells Python not
to give backslashes in the string literal any special treatment. Since
there are no backslashes in "%s", the 'r' does nothing.

I assumed the OP was trying to do the string substitution within a
path. If the OP is instead doing as you think, then you are quite
correct.
>
Therefore, (r"%s"%full) is the same as ("%s"%full), which is the same as
(full), assuming that `full` is the name of a string.

The real answer lies in fixing the code where you're assigning the
pathname to 'full', which you haven't posted. Please post the code where
you're assigning the pathname, or better yet, post the complete code
you're running.

--
Carsten Haesehttp://informixdb.sour ceforge.net

Sometimes I get too eager to help and don't do enough mental
processing before answering.

Mike
Jun 27 '08 #9
On Jun 10, 1:57*pm, Alexnb <alexnbr...@gma il.comwrote:
That would work, but not for what I want. See the file could be anywhere on
the user's system and so the entire path will be unique, and that didn't
work with a unique path. What is the subprocess module you are talking
about?

<snip>

As Carsten pointed out, we don't really know what you're doing. Or at
least, I don't. Why do you even want to do string substitution? How
does the user navigate to the files that the user wants to open? Are
you using a GUI or a command line interface?

Anyway, Google is your friend. Searching for "python subprocess" gives
you this:

http://docs.python.org/lib/module-subprocess.html

Mike
Jun 27 '08 #10

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

Similar topics

19
2985
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and provide an open TD with a DIV in it for the content of this formlet. (The DIV is for DHTML to hide and show the content) I've created a web page showing step by step the two problems I'm encountering. This problem is much easier to see than it...
6
10217
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error message: "The process cannot access the file "whatever" because it is being used by another process." I've even tried opening another file using the same streamwriter object before deleting the original file, but it's no use. Something keeps...
5
3319
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for supporting files (graphics, etc) that are stored inside the package. The problem is that the superclass's definition (stored elsewhere) has all of the code for actually opening the files, so when I use the os.path.dirname(os.path.abspath(__file__))...
1
5396
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started: Project: drawing_control, Configuration: Release Win32 ------ 1>Deleting intermediate and output files for project 'drawing_control', configuration 'Release|Win32'
2
3275
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
9
2198
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about C and some, I must admit, have not really sunk in yet. Still, I push on. Now I am taken a library of C programs that were designed to read HDF files. I work on a Unix server and in Mandrake10. The program below is most likely broken but I cannot...
1
6330
by: sunil | last post by:
Hi all, I have a web application in which I have to open a "chm" file that exists on the local file system. I am able to open other files such as PDF, DOC files by writing the Response's output stream. But I do not exactly know the Response.ContentType for chm files. I have used the following code to handle the case for chm files System.IO.StreamReader sr = new System.IO.StreamReader(this.Path); FileInfo file = new FileInfo(this.Path);...
0
927
by: saif.shakeel | last post by:
HI, Thanks for the reply.that seems to work,but i was doing this so as to attach it to a bigger code where it will be utilised before a parsing. #Input file and Output file path from user file_input = raw_input("Enter The ODX File Path:")
0
1055
by: Alexnb | last post by:
Okay, so what I want my program to do it open a file, a music file in specific, and for this we will say it is an .mp3. Well, I am using the system() command from the os class. The problem I am running into is that when I send the path of the file to the system() command, which for those of you who don't know the system command is the equivalent of typing one command into command prompt or terminal depending on your system. But let's say...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.