473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ 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_tes t\test_global.p d')
Traceback (most recent call last):
File "<string>", line 21, in ?
File "D:\data_to_tes t\Signal_WorkBe nch.py", line 118, in Write_Signal_Fi le_Ext
DataFile.Write_ Data (Data)
File "D:\data_to_tes t\Signal_WorkBe nch.py", line 95, in Write_Data
self.Write_Head er(Nchan)
File "D:\data_to_tes t\Signal_WorkBe nch.py", line 83, in Write_Header
self.datafile = open(self.filen ame,'wb')
IOError: [Errno 2] No such file or directory: 'D:\\data_to_te st\test_global. pd'
Apr 14 '07 #1
3 23010

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_tes t
\test_global .pd')
Traceback (most recent call last):
File "<string>", line 21, in ?
File "D:\data_to_tes t\Signal_WorkBe nch.py", line 118, in
Write_Signal_Fi le_Ext
DataFile.Write_ Data (Data)
File "D:\data_to_tes t\Signal_WorkBe nch.py", line 95, in
Write_Data
self.Write_Head er(Nchan)
File "D:\data_to_tes t\Signal_WorkBe nch.py", line 83, in
Write_Header
self.datafile = open(self.filen ame,'wb')
IOError: [Errno 2] No such file or directory: 'D:\\data_to_te st
\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_te st\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_te st\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_te st\\test_global .pd'
Or this:

r'D:\data_to_te st\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_te st\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_te st\\test_global .pd'
Or this:

r'D:\data_to_te st\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
8262
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 understand.)
5
6911
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 = '\/'.join(s.split('/'))
3
15875
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 : "\\x20\\x01\\x21"
2
2452
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 and Settings\ \Fred\ \My Documents\ \School\ \Bio' Is it normal that python adds the space and extra backslash? Because if I print the path of the called file before the open command, it normally prints it with only one backslash. Is there any...
6
24581
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 there is a nativi function on C++, or any algolithms I'd be glad to know about them. Thank you
2
2179
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 are left with a single backslash.
23
13521
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 = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf"; $new = preg_replace("\\", "\\\", "$var"); ?> Code above produces the following error:
35
1970
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 Windows guy) - use some kind of OS-dependent translation table if on another drive - use ? if on a network drive
6
3186
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 single quote and I cannot figure out how to get ride of it. $query = 'SELECT * FROM cpuinfo where Description like "%xcell%" or Description like "%q6600%" '; The above works perfectly but if I attempt to pass the sql string into the
0
9568
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
9399
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,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
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,...
1
7379
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
6649
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.