Dec 282009

I want to make
x=32.34565
to
32.35
How ?
round(x,2)

Posted by weboom at 7:52 pm
Dec 222009

Use writelines()
e.g.

# read lines in a file
import sys
arg1 = sys.argv[1]
f = open(arg1, ‘r’)
lines = f.readlines()
f.close()
arg=’out_’+arg1
FILE=open(arg,’w')
for line in lines:
words = line.split(’ ‘)
if words[1] == ‘hypnos’:
FILE.writelines(line)
FILE.close()

Posted by weboom at 2:53 pm Tagged with: ,
Dec 222009

When you want to find a word in a string, use find()
e.g.

import sys
arg1 = sys.argv[1] # passing the first argument
if arg1.find(”off”) == -1: # if arg1 does not contain “off”
# do something
else:
# do something

Posted by weboom at 2:36 pm Tagged with: ,
Dec 222009

Use readlines()
For example,

arg = “filename”

f = open(arg, ‘r’)

lines = f.readlines()
f.close()

Posted by weboom at 2:32 pm Tagged with: ,
Nov 292009

You can use shutil for high-level file operation (copy, move, delete etc.) in Python.
e.g.,
>>>import shutil
>>>shutil.copy(src,dest)
src is a source file.
dest is a target directory.

Posted by weboom at 10:10 pm Tagged with: ,
Nov 292009

>>>import os
>>>os.path.isdir(fileordirectoryname)
If directory, its output will be True.
If not, it will be False.

Posted by weboom at 10:08 pm Tagged with:
Nov 292009

>>>import os
>>>os.listdir(’.’)
This will make a list containing the names of files and subdirectories.

Posted by weboom at 10:05 pm Tagged with:
Nov 292009

>>>import os
>>>os.chdir(’..’)
>>>os.chdir(’/home/weboom/’)

Posted by weboom at 9:59 pm Tagged with: , ,
Nov 282009

>python program.py arg1 arg2
To get arg1 and arg2
——-
import sys
arg1 = sys.argv[1]
arg2 = sys.argv[2]

Posted by weboom at 3:21 am Tagged with: ,
Nov 182009

You will get this error when there is no list element to access.
For example,
L=[1, 2, 3]
L[4] doesn’t exist.
If you try to call L[4], you will get the above error.

Posted by weboom at 5:25 pm Tagged with: ,
Switch to our mobile site
Page 1 of 41234»Top Footer