First, create a new file with Python, which is an integer from 0 to 9 with one row per digit:
The code is as follows:
#python
>>>f=open (' F.txt ', ' W ') # R Read Only, W writable, a append
>>>for i in range (0,10): F.write (str (i) + ' \ n ')
. . .
>>> F.close ()
Second, the contents of the file appended, from 0 to 9 of the 10 random integers:
The code is as follows:
#python
>>>import Random
>>>f=open (' F.txt ', ' a ')
>>>for i in range (0,10): F.write (str (random.randint (0,9)))
. . .
>>>f.write (' \ n ')
>>>f.close ()
Third, the contents of the file appended, from 0 to 9 of the random integer, 10 digit line, a total of 10 lines:
The code is as follows:
#python
>>> Import Random
>>> f=open (' f.txt ', ' a ')
>>> for I in Range (0,10):
... for I in range (0,10): F.write (str (random.randint (0,9))
... f.write (' \ n ')
. . .
>>> F.close ()
Iv. directing the standard output to a file:
The code is as follows:
#python
>>> Import Sys
>>> sys.stdout = open ("Stdout.txt", "W")
Example:
View the 22 port condition and write the result to A.txt
The code is as follows:
#!/usr/bin/python
#coding =utf-8
Import OS
Import time
Import Sys
F=open (' a.txt ', ' a ')
F.write (Os.popen (' NETSTAT-NLTP | grep). READ ())
F.close ()