473,804 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

open filename with spaces in path

I'm having trouble opening a file in linux, whose path has spaces in it.

$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
>>open('./my test/test.txt')
Exception
>>open('./my\\ test/test.txt')
Exception

but yet...
>>import os
os.chdir('./my test')
open('./test')
works just fine.
Jun 27 '08 #1
8 37250
Michael Robertson schrieb:
I'm having trouble opening a file in linux, whose path has spaces in it.

$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
>>>open('./my test/test.txt')
Exception
Works for me
>>open('./my test/test.txt')
<open file './my test/test.txt', mode 'r' at 0xb7dd6a40>

Christian

Jun 27 '08 #2
On 2008-05-06 Michael Robertson wrote:
I'm having trouble opening a file in linux, whose path has
spaces in it.

$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
>open('./my test/test.txt')
Exception
That's funny. These exact steps work fine for me on Linux, with
Python 2.5.2, and even 1.5.2.

Does 'os.path.normpa th' have anything interesting to say about
what you're passing it?
but yet...
>import os
os.chdir('./my test')
open('./test')

works just fine.
How doctored up is this example? In the above ./test should not
actually exist. Did the chdir actually work? Or did you remove
the .txt?

--
Micah Elliott | md*@MicahElliot t.com | http://MicahElliott.blogspot.com
Jun 27 '08 #3
Michael Robertson wrote:
I'm having trouble opening a file in linux, whose path has spaces in it.

$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
>open('./my test/test.txt')
Exception
This works just fine for me. No need to escape the spaces.

You haven't given us much to work with -- tell us what exception you
get. (Or just look at it carefully yourself.) Whatever the problem
is, I don't believe it has anything to do with the spaces.

Gary Herron

>
>open('./my\\ test/test.txt')
Exception
but yet...
>import os
os.chdir('./my test')
open('./test')

works just fine.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson
<mc*********@ho tmail.comwrote:
I'm having trouble opening a file in linux, whose path has spaces in it.

$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
>>open('./my test/test.txt')
Exception
>>open('./my\\ test/test.txt')
Exception
Try a string literal by prefixing your path string with "r":

open(r'./my test/test.txt')

See http://docs.python.org/ref/strings.html
but yet...
>>import os
>>os.chdir('./my test')
>>open('./test')

works just fine.
Couldn't test on Linux, but in Windows ...
>>os.chdir('C:\ temp\my test')
Traceback (most recent call last):
File "<interacti ve input>", line 1, in <module>
WindowsError: [Error 123] The filename, directory name, or volume label
syntax is incorrect: 'C:\temp\\my test'

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #5
On Wed, 07 May 2008 09:09:08 +1000, Kam-Hung Soh <ka*********@gm ail.com>
wrote:
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson
<mc*********@ho tmail.comwrote:
>I'm having trouble opening a file in linux, whose path has spaces in it.

$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
> >>open('./my test/test.txt')
Exception
> >>open('./my\\ test/test.txt')
Exception

Try a string literal by prefixing your path string with "r":

open(r'./my test/test.txt')

See http://docs.python.org/ref/strings.html
>but yet...
> >>import os
os.chdir('./my test')
open('./test')

works just fine.

Couldn't test on Linux, but in Windows ...
>>>os.chdir('C: \temp\my test')
Traceback (most recent call last):
File "<interacti ve input>", line 1, in <module>
WindowsError: [Error 123] The filename, directory name, or volume label
syntax is incorrect: 'C:\temp\\my test'
Oops. Should have been:
>>os.chdir(r'C: \temp\my test')
(no errors)
--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #6
On May 7, 1:09*am, "Kam-Hung Soh" <kamhung....@gm ail.comwrote:
On Wed, 07 May 2008 08:36:35 +1000, Michael Robertson *

<mcrobert...@ho tmail.comwrote:
I'm having trouble opening a file in linux, whose path has spaces in it.
$ mkdir my\ test
$ echo test my\ test/test.txt
$ python
*>>open('./my test/test.txt')
Exception
*>>open('./my\\ test/test.txt')
Exception

Try a string literal by prefixing your path string with "r":

open(r'./my test/test.txt')

Seehttp://docs.python.org/ref/strings.html
but yet...
*>>import os
*>>os.chdir('./my test')
*>>open('./test')
works just fine.

Couldn't test on Linux, but in Windows ...
>os.chdir('C:\t emp\my test')

Traceback (most recent call last):
* *File "<interacti ve input>", line 1, in <module>
WindowsError: [Error 123] The filename, directory name, or volume label *
syntax is incorrect: 'C:\temp\\my test'

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
>>os.chdir('C :/')
os.getcwd()
'C:\\'
>>os.chdir('C:\ temp\my test')
Traceback (most recent call last):
File "<input>", line 1, in <module>
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect: 'C:\temp\\my test'
>>os.chdir('C :/temp/my test')
os.getcwd()
'C:\\temp\\my test'

Windoze works fine if you hand it forward slashes.
>>os.chdir('C :/')
os.chdir(os.p ath.join('C:',' temp','my test'))
os.getcwd()
'C:\\temp\\my test'

And joining your target with os.path.join works to.
Jun 27 '08 #7
On 2008-05-07, Chris <cw****@gmail.c omwrote:
>>>os.chdir('C: \temp\my test')
Traceback (most recent call last):
File "<input>", line 1, in <module>
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect: 'C:\temp\\my test'
Python strings have \ escapes, such as \t, \n etc. Above you try to go to

C:<TAB>emp\my test

and you are lucky that \m is not something special.
Either escape your back slashes ("C:\\temp\\ my test") or use raw strings
(r"C:\temp\my test").

Sincerely,
Albert
Jun 27 '08 #8
On May 7, 3:25*pm, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
On 2008-05-07, Chris <cwi...@gmail.c omwrote:
>>os.chdir('C:\ temp\my test')
Traceback (most recent call last):
* File "<input>", line 1, in <module>
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect: 'C:\temp\\my test'

Python strings have \ escapes, such as \t, \n etc. Above you try to go to

C:<TAB>emp\my test

and you are lucky that \m is not something special.

Either escape your back slashes ("C:\\temp\\ my test") or use raw strings
(r"C:\temp\my test").

Sincerely,
Albert
My personal preference is using backslashes, if it's something quick
or if it's something that will be sitting on a server in a dark corner
for a long time to just use os.path.join. I've had neither of those 2
break on me. ;)
Jun 27 '08 #9

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

Similar topics

2
23390
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string using '/' as delimiters and get the last 'word'. The reason I'm not entirely happy with that method is that it is platform specific. I would prefer to use a built in method if possible. Cheers,
1
2685
by: Thomas Anderson | last post by:
I have several large reports that I would like to produce to PDF's on a daily basis. I have been looking around to find a method to automatically pass the filename and path for these documents. Currently, when I print to PDF, I'm prompted for a file name and path. I have messed around with Ken Getz utility to automatically change the default printer and this all works fine. From what I understand, the method he describes to pass this...
5
18254
by: José Joye | last post by:
I have a instance of a StreamWriter and I need to get the path and filename to which it is attached to. This sounds easy.... I feel a bit idiot not finding it! José
1
7733
by: Andres Romero | last post by:
Using the FielDialog class and it's FileName propertie I can know the Path and file name selected by the user, but now I need to know only the file name without the full path (eg. "C:\folder1\folder2\filename.txt", I just need the "filename.txt"), how can I do it?
2
2001
by: Sean | last post by:
Hi, all - I am having trouble running a macro in Access 2000. In an action within the macro, it is set up to import data from an excel file. When I try to run it, I get an error message: The Microsoft Jet database enginecould not find the object '(filename)'. Make sure the object exists and that you spell its name and the path name correctly. This macro works fine on the computer of the person sending it to me, but when I unzip it,...
8
2826
by: PvtPile | last post by:
I've got a form where i need to pull both the directory of a filename and the directory with the filename of an .inf file... I figured the easiest way to do this was to have an <input id=test type=file runat=server> control for the user to find the .inf file, and then make vb cut off the filename upon submission of the form. I just can't figure out how to trim off the filename leaving just the directory it's located in. just as a test...
4
17796
by: ano | last post by:
Hi, I tried to use to code to get a file name from a file path but it's working in all case. string t12 = "\"C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Binn\\sqlservr.exe"; string p12 = @"^.*\\"; string r12 = Regex.Replace(t12, p12, "");
1
4758
by: newton2000 | last post by:
How can I use the preprocessor functions to get the __FILE__ macro only with the filename (without the path). The reason is that the objects are different when complied on different machines (althgouh the code has not changed). any ideas? any alternatives will be fine as well.
1
6214
by: zmohamed | last post by:
I'm passing an ofstream object to a function within my program. In that function, I'd like to extract the path and filename of the ofstream object, modify the filename only, create a new ofstream object using the old path and new filename and write something to it. How do I get the path and filename of a passed ofstream object? std::ostream& Func::writeOut(std::ostream& out) { std::string sPath = getPath(out) // how do I implement...
0
9716
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
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
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
9177
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
7643
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
6870
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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
3837
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.