472,348 Members | 1,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Double backslash in filepaths ?

It looks like sometimes a single backslash is replaced by a double backslash,
but sometimes it's not ???
See the error message below,
the first backslash is somewhere (not explicitly in my code) replaced,
but the second is not ???
Is it in general better to use double backslash in filepaths ?

thanks,
Stef Mientki

>>Write_Signal_File_Ext (IOO, fSamp, 'D:\data_to_test\test_global.pd')
Traceback (most recent call last):
File "<string>", line 21, in ?
File "D:\data_to_test\Signal_WorkBench.py", line 118, in Write_Signal_File_Ext
DataFile.Write_Data (Data)
File "D:\data_to_test\Signal_WorkBench.py", line 95, in Write_Data
self.Write_Header(Nchan)
File "D:\data_to_test\Signal_WorkBench.py", line 83, in Write_Header
self.datafile = open(self.filename,'wb')
IOError: [Errno 2] No such file or directory: 'D:\\data_to_test\test_global.pd'
Apr 14 '07 #1
3 22700

On Apr 14, 2007, at 4:26 AM, Stef Mientki wrote:
It looks like sometimes a single backslash is replaced by a double
backslash,
but sometimes it's not ???
See the error message below,
the first backslash is somewhere (not explicitly in my code) replaced,
but the second is not ???
Is it in general better to use double backslash in filepaths ?

thanks,
Stef Mientki

>>>Write_Signal_File_Ext (IOO, fSamp, 'D:\data_to_test
\test_global.pd')
Traceback (most recent call last):
File "<string>", line 21, in ?
File "D:\data_to_test\Signal_WorkBench.py", line 118, in
Write_Signal_File_Ext
DataFile.Write_Data (Data)
File "D:\data_to_test\Signal_WorkBench.py", line 95, in
Write_Data
self.Write_Header(Nchan)
File "D:\data_to_test\Signal_WorkBench.py", line 83, in
Write_Header
self.datafile = open(self.filename,'wb')
IOError: [Errno 2] No such file or directory: 'D:\\data_to_test
\test_global.pd'
If I remember correctly, you don't really have to use backslashes at
all. I think that's just a holdover from when DOS filesystems first
became hierarchical -- and they had already used the sensible
directory delimiter '/' as a command line switch character. So I
think you can just substitute forward slashes and forget that double-
backslash madness.

But that's not really answering your question, is it?

What you're looking for is called 'escape characters'. The single
backslash combines with the 't' to become a TAB character. The
double backslashes combine to become '\'. So:
>>print 'D:\\data_to_test\test_global.pd'
D:\data_to_test est_global.pd

hth,
Michael

Apr 14 '07 #2
Stef Mientki <S.**************@mailbox.kun.nlwrites:
It looks like sometimes a single backslash is replaced by a double backslash,
but sometimes it's not ???
See the error message below,
the first backslash is somewhere (not explicitly in my code) replaced,
but the second is not ???
Is it in general better to use double backslash in filepaths ?
[...]
IOError: [Errno 2] No such file or directory: 'D:\\data_to_test\test_global.pd'
'\t' is the tab character. '\d' is not a valid escape sequence, so
the backslash there survives intact. repr() normalises the string on
output, so the (single!) backslash in '\d' is displayed, as always in
the repr of strings, as '\\'.

You should either use this:

'D:\\data_to_test\\test_global.pd'
Or this:

r'D:\data_to_test\test_global.pd'

See also:

http://www.python.org/doc/faq/genera...th-a-backslash
Or even this, which will work unless you're using crufty software that
doesn't like slash path separators (cmd.exe being one of those pieces
of software):

'D:/data_to_test/test_global.pd'
John
Apr 14 '07 #3
John J. Lee wrote:
Stef Mientki <S.**************@mailbox.kun.nlwrites:
>It looks like sometimes a single backslash is replaced by a double backslash,
but sometimes it's not ???
See the error message below,
the first backslash is somewhere (not explicitly in my code) replaced,
but the second is not ???
Is it in general better to use double backslash in filepaths ?
[...]
>IOError: [Errno 2] No such file or directory: 'D:\\data_to_test\test_global.pd'

'\t' is the tab character. '\d' is not a valid escape sequence, so
the backslash there survives intact. repr() normalises the string on
output, so the (single!) backslash in '\d' is displayed, as always in
the repr of strings, as '\\'.

You should either use this:

'D:\\data_to_test\\test_global.pd'
Or this:

r'D:\data_to_test\test_global.pd'

See also:

http://www.python.org/doc/faq/genera...th-a-backslash
Or even this, which will work unless you're using crufty software that
doesn't like slash path separators (cmd.exe being one of those pieces
of software):

'D:/data_to_test/test_global.pd'
John
thanks John and Michael,
this is a very helpful explanation.

cheers,
Stef
Apr 14 '07 #4

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

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with...
5
by: Aloysio Figueiredo | last post by:
I need to replace every ocurrence of '/' in s by '\/' in order to create a file named s. My first attempt was: s = '\/'.join(s.split('/')) ...
3
by: Vincent Texier | last post by:
Hello, I want to send 3 chars in hexa code to the serial port. So I type in a tkinter entry : "\x20\x01\x21" The string is set in a...
2
by: Fred | last post by:
Hi I am writing on an application, that is supposed to read a file into a single string: My program though, when I run it, gives me an error, that...
6
by: Mikheil | last post by:
Hello! I need to translate file destination name with one backslashes "c:\program files\directory\file.txt" to string containing double...
2
by: Tobiah | last post by:
>>"'" "'" "'" "\\'" "\\'" This is quite different than any other language that I am used to. Normally, a double backslash takes away the...
23
by: dkirkdrei | last post by:
I am having a bit of trouble trying to double up on slashes in a file path. What I am trying to do is very similar to the code below: <? $var =...
35
by: Stef Mientki | last post by:
hello, I (again) wonder what's the perfect way to store, OS-independent, filepaths ? I can think of something like: - use a relative path if...
6
by: Joseph Stateson | last post by:
I just started calling a php module from html. I added "php rocket" from microsoft to FP2003 but dont think that is the cause. The problem is...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.