Saving (unusual) linux filenames

2010-08-31 Thread AmFreak

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Saving (unusual) linux filenames

2010-08-31 Thread AmFreak

Thanks for all the nice answers!


The normal thing to do is to escape the delimiter when it appears in
data.  There are lots of plenty of escaping standards to choose from,
and some of them (e.g. the one used for URLs) are already present
in various bits of Python's standard library.


The CSV module has something like that, but im using unicode and it  
doesn't work with that.





Why is your impression that the null character is "dirty"?
E.g. that's how find|xargs etc. usually work.
Another alternative would be if you gaurantee that your varn's don't
have commas then put the path last.  But that doesn't account for
filenames containing newlines.
Another alternative would be to wrap with some kind of serialization
library. But really, what's so dirty about null?


I think i just prefer a little formated file instead of one lng row :)




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

Thanks, just tried it - so simple, but seems to work like a charm. Really  
aprecciated :D.

--
http://mail.python.org/mailman/listinfo/python-list


Converting an ugly path to a shell path

2010-09-13 Thread AmFreak

Hi,

im using a QFileDialog to let the user select a path that is used later in  
a command send to the shell like this:


retcode = Popen(command + " " + path, shell=True, stdout = PIPE, stderr =  
PIPE)


The problem that occurs now is when the user selects an "ugly" path like  
this /home/user/!" ยง$/.
The shell don't understand the special chars so i have to escape them with  
"\" .

Is there a function that does this ?
If there isn't i would use a RegEx but I can't even seem to find a list  
containing all special chars :/


Greetings

AmFreak
--
http://mail.python.org/mailman/listinfo/python-list


Getting returncode of a command executed with Popen through xterm

2010-10-18 Thread AmFreak

Hi,

i have a program that have to execute linux commands. I do it like this:

retcode = Popen(["xterm", "-e", command],stdin=PIPE, stdout=PIPE,  
stderr=PIPE)


I have to use xterm because some commands need further input from the user  
after they are executed.
But when i use xterm i can't get the returncode or the errormessage from a  
command:


print retcode.returncode# always 0
print retcode.stderr.read() # always empty
print retcode.stdout.read() # always empty

The same code works without xterm. As i understand it, if i use xterm the  
retcode refers to the xterm window (process).
But is there a way i can get the returncode and errormessage of the  
command i sent to xterm ?


Thanks for any answers

AmFreak
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting returncode of a command executed with Popen through xterm

2010-10-19 Thread AmFreak

Am 19.10.2010, 10:10 Uhr, schrieb Diez B. Roggisch :


[email protected] writes:


Hi,

i have a program that have to execute linux commands. I do it like this:

retcode = Popen(["xterm", "-e", command],stdin=PIPE, stdout=PIPE,
stderr=PIPE)

I have to use xterm because some commands need further input from the
user after they are executed.
But when i use xterm i can't get the returncode or the errormessage
from a command:

print retcode.returncode# always 0
print retcode.stderr.read() # always empty
print retcode.stdout.read() # always empty

The same code works without xterm. As i understand it, if i use xterm
the retcode refers to the xterm window (process).
But is there a way i can get the returncode and errormessage of the
command i sent to xterm ?


You could create a python-wrapper-script that will store the result and
streams in files. Like this


command = ["callwrapper", "--dest-key=",  
"the_real_command"]

Popen(["xterm", "-e", command])

The dest-key will be used to create files named .status,
.stdout, .stderr so that you can read from
them afterwards.

Diez



Thanks for the answer,

but i really don't know how to do it - i never wrote a wrapper, how do i  
get the status, stdout and stderr ?
Can you elaborate that further or do you maybe have a link that could help  
me ?

--
http://mail.python.org/mailman/listinfo/python-list