I want to make
x=32.34565
to
32.35
How ?
round(x,2)
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()
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
Use readlines()
For example,
arg = “filename”
f = open(arg, ‘r’)
lines = f.readlines()
f.close()
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.
>>>import os
>>>os.path.isdir(fileordirectoryname)
If directory, its output will be True.
If not, it will be False.
>>>import os
>>>os.listdir(’.’)
This will make a list containing the names of files and subdirectories.
>>>import os
>>>os.chdir(’..’)
>>>os.chdir(’/home/weboom/’)
>python program.py arg1 arg2
To get arg1 and arg2
——-
import sys
arg1 = sys.argv[1]
arg2 = sys.argv[2]
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.
