I want to make
x=32.34565
to
32.35
How ?
round(x,2)
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 argumentif 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.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]