Saving (unusual) linux filenames

Arnaud Delobelle arnodel at googlemail.com
Tue Aug 31 21:12:41 CEST 2010


AmFreak at web.de writes:

> Hi,
>
> i have a script that reads and writes linux paths in a file. I save
> the path (as unicode) with 2 other variables. I save them seperated by
> "," and  the "packets" by newlines. So my file looks like this:
> path1, var1A, var1B
> path2, var2A, var2B
> path3, var3A, var3B
> ....
>
> this works for "normal" paths but as soon as i have a path that does
> include a "," it breaks. The problem now is that (afaik) linux allows
> every char (aside from "/" and null) to be used in filenames. The only
> solution i can think of is using null as a seperator, but there have
> to a  cleaner version ?
>
> Thanks for any help
>
> Biene_Maja

A simple solution would be to save each line of data using JSON with the json
module:

>>> import json
>>> 
>>> 
>>> path = "x,y,z"
>>> varA = 12
>>> varB = "abc"
>>> line = json.dumps([path, varA, varB])
>>> print line
["x,y,z", 12, "abc"]
>>> loadpathA, loadvarA, loadvarB = json.loads(line)
>>> print loadpathA
x,y,z
>>> print loadvarA
12
>>> print loadvarB
abc

HTH

-- 
Arnaud



More information about the Python-list mailing list