Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 - Fatal编程技术网

使用Python动态读取.txt文件

使用Python动态读取.txt文件,python,file,Python,File,我有一个文本文件,它将根据用户在php中选择的单选按钮填充 我从同一个文本文件中读取,每当我发现添加了新行时,Python脚本就会打印它。现在我只是把它打印出来。最后,我将根据.txt文件中的新记录在GSM调制解调器上发送消息。现在我只是把它打印出来,以确保我能识别出一张新唱片 这是我正在使用的代码。它很好用 def main(): flag = 0 count = 0 while flag == 0: f = open('test.txt', 'r')

我有一个文本文件,它将根据用户在php中选择的单选按钮填充

我从同一个文本文件中读取,每当我发现添加了新行时,Python脚本就会打印它。现在我只是把它打印出来。最后,我将根据.txt文件中的新记录在GSM调制解调器上发送消息。现在我只是把它打印出来,以确保我能识别出一张新唱片

这是我正在使用的代码。它很好用

def main():
    flag = 0
    count = 0
    while flag == 0:
        f = open('test.txt', 'r')
        with open('test.txt') as f:
            for i, l in enumerate(f):
                pass
        nol = i + 1
        if count < nol:
            while count <= nol:
                f = open('test.txt', 'r')
                a = f.readlines(count)
                if count == 0:
                    print a[0]
                    count = count+2
                else:
                    print a[count-1]
                    count = count+1
if __name__ ==  '__main__':
    main()
def main():
标志=0
计数=0
当标志==0时:
f=打开('test.txt','r')
将open('test.txt')作为f:
对于枚举(f)中的i,l:
通过
nol=i+1
如果计数watchdog
包监视文件中的更改。或者,您可以在Unix中使用自定义的
fcntl


第一种解决方案具有跨平台的优势。

也许这更适合codereview stackexchange?此问题的可能重复内容似乎与主题无关;你可以在