Re:由于文件路径而导致打开文件的问题 [英] Re: problems with opening files due to file's path

查看:94
本文介绍了Re:由于文件路径而导致打开文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Gerhard H?¤ring写道:



Alexnb写道:


>好的,所以我希望我的程序能够打开一个文件,特定的音乐文件,为此我们会说它是.mp3。好吧,我正在使用os类的
system()命令。 [...]

系统(\C:\Documents and Settings \Alex \ My Documents \ Myy /> Music\Rhapsody\Bryanbros \\ Weezer \(2001)\ 04 - Island In The Sun.wma \"")
[...]



试试os.startfile()代替。它应该会更好。


- Gerhard


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



不,它没有用,但是当我在shell中运行它时,它给了我一些有趣的反馈

。下面是它告诉我的:


>> os.startfile(" C: \ Documents and Settings\Alex\My Documents\My
Music\Rhapsody \ Bryanbros \ Jason Mraz \I''m Yours(Single)\ 01 - 我是
Yours.wma")



Traceback(最近一次调用最后一次):

文件" < pyshell#10>",第1行,在< module>

os.startfile(" C:\Documents and Settings \ Alex \ MyMy Documents \ Myy

Music\Rhapsody \ Bryanbros \ Jason Mraz;我是你的(单身)\ 01 - 我是Yours.wma")


WindowsError:[错误2]系统找不到指定的文件:

" C:\\Documents and Settings \\Alex \\ Myy Documents \\我的

Music\\Rhapsody \\ Bryanbros \\\ Jason Mraz \\I''m Yours(Single)\ x01 - 我是

Yours.wma"


见它将每个反斜杠分成两个,括号中的那个和

0变成了一个x ....

-

在上下文中查看此消息: http://www.nabble.com/problems-with-...p17759825.html

从Nabble的Python-python-list邮件列表存档发送。 com。

解决方案

6月10日11:45 * am,Alexnb< alexnbr ... @ gmail.comwrote:


Gerhard H?ring写道:


Alexnb写道:


好​​的,所以我希望我的程序能够打开一个文件,特定于

的音乐文件,为此我们会说它是.m P3。好吧,我正在使用os类中的

system()命令。 [...]


system(" \" C:\Documents和设置\ Alex \我的文件\我/

音乐\狂想曲'\\ Bryanbros \ Weezer \(2001)\ 04 - Island In The Sun.wma \"" ;)

[...]


请尝试使用os.startfile()。它应该更好。


- Gerhard


-
< a rel =nofollowhref =http://mail.python.org/mailman/listinfo/python-listtarget =_ blank> http://mail.python.org/mailman/listinfo/python-list



不,它没有用,但是当我在shell中运行它时,它给了我一些有趣的反馈

。下面是它告诉我的:


> os.startfile(" C:\Documents and Settings\Alex\我的文件\ MyMy
Music\Rhapsody \ Bryanbros \ Jason Mraz \我是你的(单身)\ 01 - 我是
Yours.wma")



回溯(最近一次调用最后一次):

*文件"< pyshell#10>",第1行,in < module>

* * os.startfile(" C:\Documents and Settings\Alex\My Documents\My

Music\Rhapsody \\ \\ Bryanbros \ Jason Mraz \我是你的(单身)\ 01 - 我是你的.wma")


WindowsError:[错误2]系统不能找到指定的文件:

" C:\\Documents and Settings \\Alex \\ My Documents \\My

Music \\ \\\ Rhapsody \\Bryanbros \\ Jason Mraz \\I''Y You rs(Single)\ x01 - 我是

Yours.wma"


看到它将每个反斜杠分成两个,括号中的一个并且

0变成了x ....

-

在上下文中查看此消息: http://www.nabble.com/problems-with -... o-file%27s-pat ...

从Nabble.com的Python-python-list邮件列表存档发送。



是的。您需要将所有反斜杠加倍,或者通过添加r来使其成为原始的

字符串。一开始,就像这样:


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

>
HTH


Mike




嘿谢谢!,原始和双反斜杠工作。你是一个绅士和一个学者。


Mike Driscoll写道:



6月10日上午11:45,Alexnb< alexnbr。 .. @ gmail.comwrote:


> Gerhard H?¤ring写道:


Alexnb写道:
好的,所以我希望我的程序能够打开一个文件,一个特定的音乐文件,为此我们会说它是一个.mp3。好吧,我正在使用os类的
system()命令。 [...]


> system(" \" C:\Documents and Settings \Alex\ My Documents \\ \\ Myy
Music\Rhapsody \ Bryanbros \ Weezer \(2001)\ 04 - Island In The Sun.wma \"
[...] $ ... $ b


请尝试使用os.startfile()。它应该更好。


- Gerhard


-
< a rel =nofollowhref =http://mail.python.org/mailman/listinfo/python-listtarget =_ blank> http://mail.python.org/mailman/listinfo/python-list


不,它没有用,但是当我在shell中运行
它时,它给了我一些有趣的反馈。下面是它告诉我的内容:


>> os.startfile(" C:\Documents and Settings \ Alex \ MyMy Documents \ My
Music\Rhapsody \ Bryanbros \ Jason Mraz \我是你的(单身)\ 01 - 我是
Yours.wma")

<回溯(最近一次调用最后一次):
文件"< pyshell#10>",第1行,< module>
os.startfile(" C:\Documents和设置\ Alex \我的文件\我/
Music\Rhapsody \ Bryanbros \ Jason Mraz \我是你的(单身)\ 01 - 我是你的
你的.wma")

WindowsError:[错误2]系统找不到指定的文件:
C:\\Documents and Settings \\Alex \\我的文件\\\\My
音乐\\Rhapsody \\Bryanbros \\ Jason Mraz \\I''m Yours(Single)\ x01 - 我是
Yours.wma"

看看每个反斜杠分为两个,一个由括号和
0变成一个x ....
-

上下文中查看此消息:< a rel =nofollowhref =http://www.nabble.com/problems-with-opening-files-due-to-file%27s-pattarget =_ blank> http://www.nabble .com / problems-with -... o-file%27s-pat ...
从Nabble.com的Python-python-list邮件列表存档发送。



是的。您需要将所有反斜杠加倍,或者通过添加r来使其成为原始的

字符串。一开始,就像这样:


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

>
HTH


Mike

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



-

在上下文中查看此消息: http://www.nabble.com/problems-with-...p17761126.html

从Nabble.com的Python-python-list邮件列表存档发送。




嗯,现在我已经遇到另一个问题,这次是路径将是一个

变量,而我无法弄清楚如何使startfile()使其成为原始的

变量,如果我把startfile(r变量),它不起作用和

startf ile(rvariable)显然不会工作,你知道如何使这项工作

或更好,如何获取一个给定的常规字符串并使每一个

单&\\成为双重的\\ -


Mike Driscoll写道:



On Jun 10点,11点45分,Alexnb< alexnbr ... @ gmail.comwrote:


> Gerhard H?¤ring写道:


Alexnb写道:
好的,所以我希望我的程序能够打开一个文件,一个特定的音乐文件,为此我们会说它是一个.mp3。好吧,我正在使用os类的
system()命令。 [...]


> system(" \" C:\Documents and Settings \Alex\ My Documents \\ \\ Myy
Music\Rhapsody \ Bryanbros \ Weezer \(2001)\ 04 - Island In The Sun.wma \"
[...] $ ... $ b


请尝试使用os.startfile()。它应该更好。


- Gerhard


-
< a rel =nofollowhref =http://mail.python.org/mailman/listinfo/python-listtarget =_ blank> http://mail.python.org/mailman/listinfo/python-list


不,它没有用,但是当我在shell中运行
它时,它给了我一些有趣的反馈。下面是它告诉我的内容:


>> os.startfile(" C:\Documents and Settings \ Alex \ MyMy Documents \ My
Music\Rhapsody \ Bryanbros \ Jason Mraz \我是你的(单身)\ 01 - 我是
Yours.wma")

<回溯(最近一次调用最后一次):
文件"< pyshell#10>",第1行,< module>
os.startfile(" C:\Documents和设置\ Alex \我的文件\我/
Music\Rhapsody \ Bryanbros \ Jason Mraz \我是你的(单身)\ 01 - 我是你的
你的.wma")

WindowsError:[错误2]系统找不到指定的文件:
C:\\Documents and Settings \\Alex \\我的文件\\\\My
音乐\\Rhapsody \\Bryanbros \\ Jason Mraz \\I''m Yours(Single)\ x01 - 我是
Yours.wma"

看看每个反斜杠分为两个,一个由括号和
0变成一个x ....
-

上下文中查看此消息:< a rel =nofollowhref =http://www.nabble.com/problems-with-opening-files-due-to-file%27s-pattarget =_ blank> http://www.nabble .com / problems-with -... o-file%27s-pat ...
从Nabble.com的Python-python-list邮件列表存档发送。



是的。您需要将所有反斜杠加倍,或者通过添加r来使其成为原始的

字符串。一开始,就像这样:


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

>
HTH


Mike

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



-

在上下文中查看此消息: http://www.nabble.com/problems-with-...p17761338.html

从Nabble.com的Python-python-list邮件列表存档发送。



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\My Documents\My
Music\Rhapsody\Bryanbros\Weezer\(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\My Documents\My
Music\Rhapsody\Bryanbros\Jason Mraz\I''m Yours (Single)\01 - I''m
Yours.wma")

Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
os.startfile("C:\Documents and Settings\Alex\My 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:\\Documents and Settings\\Alex\\My Documents\\My
Music\\Rhapsody\\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-...p17759825.html
Sent from the Python - python-list mailing list archive at Nabble.com.

解决方案

On Jun 10, 11:45*am, Alexnb <alexnbr...@gmail.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\My Documents\My
Music\Rhapsody\Bryanbros\Weezer\(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\My Documents\My
Music\Rhapsody\Bryanbros\Jason Mraz\I''m Yours (Single)\01 - I''m
Yours.wma")


Traceback (most recent call last):
* File "<pyshell#10>", line 1, in <module>
* * os.startfile("C:\Documents and Settings\Alex\My 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:\\Documents and Settings\\Alex\\My Documents\\My
Music\\Rhapsody\\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\my\file'')

HTH

Mike



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...@gmail.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\My Documents\My
Music\Rhapsody\Bryanbros\Weezer\(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\My Documents\My
Music\Rhapsody\Bryanbros\Jason Mraz\I''m Yours (Single)\01 - I''m
Yours.wma")


Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
os.startfile("C:\Documents and Settings\Alex\My 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:\\Documents and Settings\\Alex\\My Documents\\My
Music\\Rhapsody\\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\my\file'')

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.



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(rvariable) 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...@gmail.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\My Documents\My
Music\Rhapsody\Bryanbros\Weezer\(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\My Documents\My
Music\Rhapsody\Bryanbros\Jason Mraz\I''m Yours (Single)\01 - I''m
Yours.wma")


Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
os.startfile("C:\Documents and Settings\Alex\My 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:\\Documents and Settings\\Alex\\My Documents\\My
Music\\Rhapsody\\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\my\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.


这篇关于Re:由于文件路径而导致打开文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆