python中的file是什么意思呢?

Python011

python中的file是什么意思呢?,第1张

是file类的构造函数,参数和内置的open()函数相同,在打开文件时更推荐使用open(),所以更多用于测试文件类型的测试:isinstance(f,file)

参考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去运行