Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在给定的代码中,我首先放置的块顺序返回结果,而我放置的块[在desc中继续]_Python_Python 3.x_File_Readlines - Fatal编程技术网

Python 在给定的代码中,我首先放置的块顺序返回结果,而我放置的块[在desc中继续]

Python 在给定的代码中,我首先放置的块顺序返回结果,而我放置的块[在desc中继续],python,python-3.x,file,readlines,Python,Python 3.x,File,Readlines,[continuation]-second将结果返回为零,为什么以及如何使两个读取函数使用相同的文件名 代码: myfile=open(r'C:\Users\win8\Documents\Cs Record\trust.txt','r') **#number of lines in the file:** r2=myfile.readlines() nol=len(r2) print('The number of lines in trust.txt is',nol) **#si

[continuation]-second将结果返回为零,为什么以及如何使两个读取函数使用相同的文件名

代码:

myfile=open(r'C:\Users\win8\Documents\Cs Record\trust.txt','r')

   **#number of lines in the file:**

r2=myfile.readlines()

nol=len(r2)

print('The number of lines in trust.txt is',nol)

**#size of the file:**

r1=myfile.read()

sof=len(r1)

print('The size of trust.txt is',sof,'bytes')

myfile.close()

使用
myfile.seek(0)

还是更好地利用

with open('file.txt') as f:
    read_data = f.read()
# file is closed now