Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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读取.txt文件会导致控制台中出现空行?_Python_File Io - Fatal编程技术网

为什么用Python读取.txt文件会导致控制台中出现空行?

为什么用Python读取.txt文件会导致控制台中出现空行?,python,file-io,Python,File Io,我正在阅读一个.txt文件的教程的一部分。我按照指令键对键操作,但我的控制台日志返回一个空行 有人知道会发生什么吗 employee_file = open('employees.txt', 'r') print(employee_file.readline()) employee_file.close() 试着这样做: with open ('employees.txt') as file : for line in file : print (line) 试着

我正在阅读一个.txt文件的教程的一部分。我按照指令键对键操作,但我的控制台日志返回一个空行

有人知道会发生什么吗

employee_file = open('employees.txt', 'r')

print(employee_file.readline())

employee_file.close()
试着这样做:

with open ('employees.txt') as file :
    for line in file :
        print (line)
试着这样做:

with open ('employees.txt') as file :
    for line in file :
        print (line)

首先,确保您在文件所在的同一路径中工作, 使用以下命令: printos.getcwd 其次,确保文件不是空的并保存它。 第三,使用@bashbelam编写的代码,它应该可以工作


我希望这对您有所帮助。

首先,确保您在文件所在的同一路径中工作, 使用以下命令: printos.getcwd 其次,确保文件不是空的并保存它。 第三,使用@bashbelam编写的代码,它应该可以工作


我希望这对您有所帮助。

在同一控制台会话中,您可能已经打开并读取了文件,但忘记了关闭它。然后重新运行同一个或多个读线,这些读线将返回空,因为文件指针已经位于文件的末尾

$cat employees.txt 吉姆销售 111 222 $python3.7 Python 3.7.2默认版本,2019年3月8日,19:01:13 [GCC 5.4.0 20160609]在linux上 有关详细信息,请键入帮助、版权、信用证或许可证。 >>>employee_file=打开'employees.txt','r' >>>printemployee_file.readline 吉姆销售 >>>printemployee_file.readlines ['111\n','222\n'] >>> >>> >>> >>>printemployee_file.readline >>>printemployee_file.readlines [] 这就是为什么建议的做法是始终:


在同一控制台会话中,您可能已经打开并读取了该文件,但忘记关闭它。然后重新运行同一个或多个读线,这些读线将返回空,因为文件指针已经位于文件的末尾

$cat employees.txt 吉姆销售 111 222 $python3.7 Python 3.7.2默认版本,2019年3月8日,19:01:13 [GCC 5.4.0 20160609]在linux上 有关详细信息,请键入帮助、版权、信用证或许可证。 >>>employee_file=打开'employees.txt','r' >>>printemployee_file.readline 吉姆销售 >>>printemployee_file.readlines ['111\n','222\n'] >>> >>> >>> >>>printemployee_file.readline >>>printemployee_file.readlines [] 这就是为什么建议的做法是始终:

使用文件对象的查找功能

这将文件的当前位置设置为偏移量,可能是文件中的光标位于最后一个位置,因此您将一无所获

更新您的代码:

employee_file = open('employees.txt', 'r')
employee_file.seek(0)  # Sets cursor in your file at absolute position (At beginning)

print(employee_file.readline())
这应该可以工作。

使用文件对象的查找功能

这将文件的当前位置设置为偏移量,可能是文件中的光标位于最后一个位置,因此您将一无所获

更新您的代码:

employee_file = open('employees.txt', 'r')
employee_file.seek(0)  # Sets cursor in your file at absolute position (At beginning)

print(employee_file.readline())

这应该会起作用。

是否您的employees.txt文件是空的?文本文件的第一行是什么?它只读取一行。你是说readlines吗?我做了readlines,它在我的控制台上打印了一个开括号和一个闭括号。该文件不是空的,我确保使用.txt文件!我的第一行是Jim-Salesopen'employees.txt',r'假定文件位于当前工作目录中。尝试给出完整路径。可能是employees.txt文件为空吗?文本文件的第一行是什么?它只读取一行。你是说readlines吗?我做了readlines,它在我的控制台上打印了一个开括号和一个闭括号。该文件不是空的,我确保使用.txt文件!我的第一行是Jim-Salesopen'employees.txt',r'假定文件位于当前工作目录中。尝试给出完整的路径。嘿,非常感谢你的帮助,非常感谢。嘿,非常感谢你的帮助,非常感谢。非常感谢你的帮助!非常感谢你的帮助!