473,624 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the perfect (OS independent) way of storing filepaths ?

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

I'm interested what you all use for this kind of problem.
And I wonder why there isn't a standard solution / library in Python
available.

thanks,
Stef Mientki
Oct 19 '08 #1
35 1946
On Oct 19, 8:35*am, Stef Mientki <stef.mien...@g mail.comwrote:
I (again) wonder what's the perfect way to store, OS-independent,
filepaths ?
I don't think there is any such thing. What problem are you trying to
solve?
Oct 19 '08 #2
On Sun, 19 Oct 2008 14:35:01 +0200, Stef Mientki wrote:
hello,

I (again) wonder what's the perfect way to store, OS-independent,
filepaths ?
"Perfect"? I can't imagine any scheme which will work on every imaginable
OS, past present and future.

However, in practice I think there are two common forms still in use:
Posix paths, and Windows paths. I believe that OS/2 can deal with Windows
pathnames, and Mac OS X uses Posix paths (I think...). If you have to
support Classic Mac OS or other non-Posix systems, then your life will
become interesting and complicated.

And let's not even consider Unicode issues...

You might find this page useful:
http://en.wikipedia.org/wiki/Path_(computing)

Note that raw strings are for regular expressions, not Windows paths. Raw
strings can't end in a backslash, so you can't do this:

r'C:\My Documents\'

Instead, you can avoid having to escape backslashes by taking advantage
of the fact that Windows will accept forward slashes as well as
backslashes as path separators, and write 'C:/My Documents/' instead.

I assume you're familiar with the path-manipulation utilities in os.path?
>>import os
os.path.split drive('C://My Documents/My File.txt')
('C:\\\\', 'My Documents\\My File.txt')

I had to fake the above output because I'm not running Windows, so excuse
me if I got it wrong.

But honestly, I think your biggest problem isn't finding a platform-
independent way of storing paths, but simply translating between each
OS's conventions on where files should be stored.

In Linux, config files should go into:

~/.<appname>/ or /etc/<appname>/

In Windows (which versions?) then should go into the Documents And
Settings folder, where ever that is.

There's no single string which can represent both of these conventions!

--
Steven
Oct 19 '08 #3
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrote:
In Linux, config files should go into:

~/.<appname>/ or /etc/<appname>/

In Windows (which versions?) then should go into the Documents And
Settings folder, where ever that is.

There's no single string which can represent both of these conventions!
The first of those should do nicely for both Linux and Windows:
>>os.path.normp ath(os.path.exp anduser('~/.appname'))
'C:\\Documents and Settings\\Dunca n\\.appname'
Oct 19 '08 #4
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrote:
>>>import os
os.path.spli tdrive('C://My Documents/My File.txt')
('C:\\\\', 'My Documents\\My File.txt')

I had to fake the above output because I'm not running Windows, so
excuse me if I got it wrong.
Not that it matters, but:
>>os.path.split drive('C://My Documents/My File.txt')
('C:', '//My Documents/My File.txt')
Oct 19 '08 #5
On 2008-10-19, Stef Mientki <st**********@g mail.comwrote:
I (again) wonder what's the perfect way to store, OS-independent,
filepaths ?
The question appears to me to be meaningless. File paths are
not OS independant, so an OS-independant way to store them
doesn't seem to be a useful thing to talk about.

--
Grant

Oct 19 '08 #6
>I (again) wonder what's the perfect way to store, OS-independent,
>filepaths ?
I'm in agreement that perfect probably isn't applicable. If I were
doing this myself, I might store the information in a tuple:

base = 'some root structure ('/' or 'C')
path = ['some','set','o f','path','name s']
filename = 'somefile.ext'

pathdata = (root,path,file name)

and write a couple of simple functions to reconstruct them based on the os.
Oct 19 '08 #7
Eric Wertman wrote:
>>I (again) wonder what's the perfect way to store, OS-independent,
filepaths ?

I'm in agreement that perfect probably isn't applicable. If I were
doing this myself, I might store the information in a tuple:

base = 'some root structure ('/' or 'C')
path = ['some','set','o f','path','name s']
filename = 'somefile.ext'

pathdata = (root,path,file name)

and write a couple of simple functions to reconstruct them based on the os.
Eric, I like your idea.
It looks like a workable technique,
the user should initial define the roots once and everything works.
It should even work for network drives and websites.

Duncan, in windows it's begin to become less common to store settings in
Docs&Settings,
because these directories are destroyed by roaming profiles (a big
reason why I can't run Picassa ;-(
It's more common to follow the portable apps approach, store them in the
application directory.

Drobinow, I want to distribute an application with a large number of
docs and examples.
Now for this application I can put everything in subpaths of the
main-application,
but you triggered me to put a warning in my code if I go outside the
application path.
Another application I've in mind, is a data manager (now written in Delpi),
in which I organize all my information: docs, websites, measurement data
etc.

Others, thank you for the ideas, you learende me some new os.path functions.

cheers,
Stef
--
http://mail.python.org/mailman/listinfo/python-list
Oct 19 '08 #8
Duncan Booth wrote:
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrote:
>In Linux, config files should go into:

~/.<appname>/ or /etc/<appname>/

In Windows (which versions?) then should go into the Documents And
Settings folder, where ever that is.

There's no single string which can represent both of these conventions!

The first of those should do nicely for both Linux and Windows:
>>>os.path.norm path(os.path.ex panduser('~/.appname'))
'C:\\Documents and Settings\\Dunca n\\.appname'
A tuple of path elements, I would think.
>>a= ( 'c:', 'windows', 'system' )
a= ( '~', 'usr', 'bin' )
a= ( '..', 'src' )
You'll want a subclass too, which has a file for the last name, instead
of just folders.
>>a= ( 'c:', 'python', 'python.exe' )
If '..' and '~' aren't universally, recognized, you'll want special
flags.
>>DirUp= type( 'DirUp', (object,), { '__repr__': ( lambda self: 'DirUp' ) } )()
a= ( DirUp, 'src' )
a
(DirUp, 'src')

As for rendering them, 'win', 'unix', and 'mac' could be methods.
Oct 19 '08 #9
On Sun, 19 Oct 2008 15:40:32 +0000, Duncan Booth wrote:
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrote:
>In Linux, config files should go into:

~/.<appname>/ or /etc/<appname>/

In Windows (which versions?) then should go into the Documents And
Settings folder, where ever that is.

There's no single string which can represent both of these conventions!

The first of those should do nicely for both Linux and Windows:
>>>os.path.norm path(os.path.ex panduser('~/.appname'))
'C:\\Documents and Settings\\Dunca n\\.appname'

Except Windows users will be wondering why they have a directory starting
with '.' in their home directory. Dot to make files hidden is not AFAIK
supported by Windows.

--
Steven
Oct 19 '08 #10

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

Similar topics

1
2613
by: Hari Shankar | last post by:
Dear all I have to develop a database independent application in c# 2003. It means the underlying database should be anything like ACCESS, Oracle or SQL and my application should not get affected if i change between databases. i also have a requirement of storing 10,000 user records in my database. This database can also be created by our customers and weshould be able to use it. After reading ADO.NET i felt convinced that it is the...
3
1551
by: Peter Hardy | last post by:
Hi guys, Sorry for the cross-post but I got no response in the asp.net newsgroup. I am trying to develop a mini e-learning application where the user provides content for each page. Eventually, I'd like to shift to using templates but at the moment the users is just entering content using html. Whats the best way to allow the user to do this and whats the best way of ensuring the html is valid before I store it in my database / xml...
1
1494
by: Carly | last post by:
Hi, I work in Visual Basic .NET 2002 using ASP.NET. I am wondering what would be the best way to store/pass variables between pages. (Sessions are sooo simple to use but I hear they are not the best....). Having a class with static parameters???? Having a class that all the pages inherit??? (These last 2 ideas are coming from the news group but I do not quite understand them).
0
8240
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
8680
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...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
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
5565
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
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.