By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

os.path.split: processing paths from one OS on another

P: n/a
Heya folks,

I ran into the following problem:

When i run this on Windows everything is as expected:
C:\>python
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import os
file='/TEST/FILE.EXT'
print os.path.split(file) ('/TEST', 'FILE.EXT') file='C:\\TEST\\FILE.EXT'
print os.path.split(file) ('C:\\TEST', 'FILE.EXT')

However, when i run this on Linux the results are unexpected:
$ python
Python 2.2.3 (#1, Nov 12 2003, 15:53:11)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-110)] on linux2
Type "help", "copyright", "credits" or "license" for more information. import os
file='/TEST/FILE.EXT'
print os.path.split(file) ('/TEST', 'FILE.EXT') file='C:\\TEST\\FILE.EXT'
print os.path.split(file)

('', 'C:\\TEST\\FILE')

All i can find is some comments on POSIX-ness (or not) of the OS, in
regard to os.path.split(), but i fail to see why that explains the
different outcome of the last command in the two examples above.

My server needs to be run on either Linux or Windows, it receives
requests from clients that may run either OS. To process those requests
i need to split the path and filename. I hoped to solves this using
os.path.split (), any suggestions as to how to fix this?

Mazzel,

Martijn.

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Martijn Ras <ra*@holmes.nl> writes:
Heya folks, I ran into the following problem: When i run this on Windows everything is as expected:
C:\>python
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import os
file='/TEST/FILE.EXT'
print os.path.split(file)('/TEST', 'FILE.EXT') file='C:\\TEST\\FILE.EXT'
print os.path.split(file)('C:\\TEST', 'FILE.EXT') However, when i run this on Linux the results are unexpected:
$ python
Python 2.2.3 (#1, Nov 12 2003, 15:53:11)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-110)] on linux2
Type "help", "copyright", "credits" or "license" for more information. import os
file='/TEST/FILE.EXT'
print os.path.split(file)('/TEST', 'FILE.EXT') file='C:\\TEST\\FILE.EXT'
print os.path.split(file)

('', 'C:\\TEST\\FILE')


Well, that is correct behaviour for any unix type machine because \ and C: are
just part of a filename, the only special character in a unix file name is /
(the directory separator). So if the user is trying to specify a file on the
unix system either they will need to use unix conventions or you will need to
do your own mapping from windowsland names to unixland names. Since Python
already does the mapping in the opposite direction (ie making everything look
like a unix name) it might make more sense to adopt this convention. Of
course, if your actual purpose is simply to manipulate filenames rather than
use the name to reference a file then you need to use the library functions
specific to the OS (in this case windows) which someone else has already
mentioned.

Eddie
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.