473,507 Members | 13,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file delimiter in dependency of os

Hallo,

I have pathes with different file delimiters
e.g.
/root/dir\mydir/myfile
C:\xxx/hh\gggg/myfile

How can I change the mistaken file delimiters in dependency of the
used os (win or unix) effectively?
unix: /root/dir\mydir --> /root/dir/mydir
win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile

Thanks for your hints Thomas
Jul 18 '05 #1
2 3221
Thomas Steffen wrote:
Hallo,

I have pathes with different file delimiters
e.g.
/root/dir\mydir/myfile
C:\xxx/hh\gggg/myfile

How can I change the mistaken file delimiters in dependency of the
used os (win or unix) effectively?
unix: /root/dir\mydir --> /root/dir/mydir
win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile

Thanks for your hints Thomas


Note that backslashes are only needed for the conventional user experience,
Windows is happy with "/", too. Have a look at os.path.normcase() which
should meet your needs if you are consistently using forward slashes in
your application. Otherwise the following tiny module might do (untested):

<plat.py>
import os
if os.name == "posix":
def normslash(path):
return path.replace("\\", "/")
elif os.name == "nt":
def normslash(path):
return path.replace("/", "\\")
else:
raise Exception("OS not supported: %s" % os.name")
</plat.py>

usage:

import plat
path = plat.normslash(path)

By the way, if you are building paths like so:

path = folder + "/" + filename

Use os.path.join() instead. It will automatically select the right separator
for you.

Peter

Jul 18 '05 #2
Peter Otten wrote:

Thomas Steffen wrote:
Hallo,

I have pathes with different file delimiters
e.g.
/root/dir\mydir/myfile
C:\xxx/hh\gggg/myfile

How can I change the mistaken file delimiters in dependency of the
used os (win or unix) effectively?
unix: /root/dir\mydir --> /root/dir/mydir
win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile

Thanks for your hints Thomas


Note that backslashes are only needed for the conventional user experience,
Windows is happy with "/", too. Have a look at os.path.normcase() which


Or, to be more practical, backslashes are needed for commands that are
passed to the MS-DOS shell with os.system() and friends. Any other
way of getting the paths to the OS should work with forward slashes.

-Peter
Jul 18 '05 #3

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

Similar topics

2
11128
by: nospam | last post by:
If I had a text file with the following two columns: key1 value1 key2 value2 key3 value3 How would I read in this file and create an associative array?
2
4344
by: Dave Stone | last post by:
Does anyone know if it's possible to use the wizard or DTS Designer to accept a source file with the following simplified format: <field1label>: <record1field1value> <field2label>: ...
11
3053
by: Skc | last post by:
I have a .txt which has been exported as a .csv from an external source. What i need to do is to import this into SQL2000 (into a table) but I need to do special things on the data: 1. I need to...
6
1761
by: Greg Collins [MVP] | last post by:
For background, please refer to my original thread: http://groups.google.com/groups?selm=efe5w7tYEHA.3112%40tk2msftngp13.phx.gbl I've thought of a potential way around the issue, but I'm new to...
8
14567
by: asrs63 | last post by:
Hi, I am a newbee and have a comma seperated flat-file and a DTD. I need to write a C# program which will read the text file and convert it to a XML file as per the the definition in the DTD. I...
5
2543
by: rjl444 | last post by:
My app has a lot of properties, instead of placing the values in webconfig file (because I have tons), I would like to place these in it's own file. What is the best way of using an independant...
2
1663
by: removethispamfilter | last post by:
Hi Im working in a company where windows systems are set up for different "countries". When setting up the "import from csv file" the decimal separator is set up to "," (the thousands...
19
5356
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
4
2111
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
0
7221
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
7109
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
7313
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,...
1
7029
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...
0
7481
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
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
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 ...
0
411
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...

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.