参考python2.7.5文档的解释:
file(name[, mode[,
buffering]])
Constructor function for the file type, described further in section File
Objects. The constructor’s arguments are the same as those of the open()
built-in function described below.
When opening a file, it’s preferable to use open()
instead of invoking this constructor directly. file
is more suited to type testing (for example, writing isinstance(f, file)).
#python
f=open('f.txt','w') # r只读,w可写,a追加
for i in range(0,10):f.write(str(i)+'\n')
例子:
#!/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()
扩展资料:关于上述创建文件,文件内容追加
#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()
或者
#python
import rando
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()
.py结尾的可以用ide(pycharm)或者txt文档打开用python语言打开文件可以用open(filename)去打开
如果运行python文件(.py结尾的文件),可以用命令python xxx.py去运行