Python creates and appends object content instances,
1. Create a new file in Python with an integer ranging from 0 to 9. Each number occupies one row:
Copy codeThe Code is as follows:
# Python
>>> F=open('f.txt ', 'w') # r read-only, w writable, a appended
>>> For I in range (0, 10): f. write (str (I) + '\ n ')
...
>>> F. close ()
2. append the file content, with 10 random integers from 0 to 9:
Copy codeThe 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 ()
3. append the file content, a random integer from 0 to 9, a row of 10 numbers, 10 rows in total:
Copy codeThe Code is as follows:
# Python
>>> Import random
>>> F=open('f.txt ', 'A ')
>>> For I in range (0, 10 ):
... For I in range (): f. write (str (random. randint )))
... F. write ('\ n ')
...
>>> F. close ()
4. Export standard output to a file:
Copy codeThe Code is as follows:
# Python
>>> Import sys
>>> Sys. stdout = open ("stdout.txt", "w ")
Example:
22nd client profile, and write the final result into a.txt
Copy codeThe 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 22 '). read ())
F. close ()