On 2015-01-23 19:24:20 -0500, Greg Freemyer wrote:
Any python programmers here?
I'm am (or was) a c programmer, but I want to learn python.
My goal is to leverage a python library I maintain (python-dfVFS).
I've just written my first python program and it actually works!
I wrote it in python 2.7, but I wonder if should skip straight to
learning python 3? The code base I want to call into is currently in
python 2.7, so I'm assuming that I should write my code in 2.7, but if
it is easy to call 2.7 code from 3.0, I think I'd rather do that.
Anyway, can someone one advise as to if I'm doing anything below in a
See the comments below.
charOld = ['"',
'\xfe','\x14'] # find these...
charNew = ['""','"',',']
# ...and replace with these
#-----Need some help?-------
print("Error: ", errMsg, file=sys.stderr)
print(sys.argv, " [input filename] [output filename]\n",
"NB: Output file will be created, overwriting any existing\n",
"file of the same name.", file=sys.stderr)
You should use sys.exit instead of os._exit. Also, it is probably
better to indicate the failure with a non zero exit status (e.g.
2 to indicate wrong arguments/usage).
You can omit this return statement.
if len(sys.argv) < 3:
Usage('Arguments are required')
I would use python's with statement, e.g.
with open(sys.argv, 'r') as input, open(sys.argv, 'w') as
The advantage is that in case of an exception, the file objects
are automatically closed.
line = input.readline()
if not line:
This could be rewritten as follows:
for line in input.readlines():
for old, new in zip(charOld, charNew):
line = line.replace(old, new)
Note: the comments above are just some suggestions:)
To unsubscribe, e-mail: opensuse-programming+unsubscribe(a)opensuse.org
To contact the owner, e-mail: opensuse-programming+owner(a)opensuse.org