import randomf = open(‘data.txt’,‘w+’)for i in range(100000):f.write(str(random.randint(1,100)) + ‘\n’)f.seek(0)print(f.read())f.close()问:创建的data.txt文件中有多少行
举一反三
- 若文件data.txt中有字符串“飞雪连天射白鹿”,则不能将"笑书神侠倚碧鸳"写入该字符串后面的语句是( ) A: f=open("data.txt",'a')f.write("笑书神侠倚碧鸳")f.close() B: f=open("data.txt",'w+')f.write("笑书神侠倚碧鸳")f.close() C: f=open("data.txt",'w+')f.writelines(["飞雪连天射白鹿","笑书神侠倚碧鸳"])f.close() D: f=open("data.txt",'w')f.write("飞雪连天射白鹿笑书神侠倚碧鸳")f.close()
- 下列代码中能够正确实现追加写入的是( )。 A: with open("d:\\python\\data.txt",'w') as f: f.write('Hello!') B: with open("d:\\python\\data.txt",'a') as f: f.write('Hello!') C: with open("d:\\python\\data.txt",'r+') as f: f.write('Hello!') D: with open("d:\\python\\data.txt",'wb+') as f: f.write('Hello!')
- 下列程序的输出结果是()。f=open('out.txt','w+')f.write('Python') f.seek(0)c=f.read(2) print() f.close() A: Pyth B: Python C: Py D: th
- 下列程序的输出结果是( )。 import os f = open(r'D:\test.txt','w+') f.write('Python') f.seek(0) m = f.read(3) print(m) f.close() A: Pyt B: Python C: yth D: Py
- f=open('c: \ \out.txt’, 'w+')f.write('Python')f.seek(0)c=f.read (2)print(c)f.close(),以上程序的输出结果是( ) A: Python B: th C: Py D: Pyth