473,466 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 22942

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 single quotes, and presumably simpler to...
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('/')) but it doesn't work: >>> s = 'a/b' >>> s =...
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 StringVar(). When I read the stringVar, I get :...
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 the called file is non existent 'C:\ \Documents...
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 backslashes "c:\\program files\\directory\\file.txt" If...
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 special meaning of the last backslash, and so you...
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 drive is identical to the application (I'm still a...
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 that I am getting a backslash before a double or...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.