Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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_Python 2.7_Wxpython_Ipython - Fatal编程技术网

在python脚本中从文件夹读取.txt文件

在python脚本中从文件夹读取.txt文件,python,python-2.7,wxpython,ipython,Python,Python 2.7,Wxpython,Ipython,我必须从文件夹中只读取一个.txt文件,所以我正在执行上述操作。后来我试着把我读到的东西打印出来。但是当我运行上面的程序时,我没有得到任何输出。有人能帮我一下吗?有什么错吗 使用以下命令: directory =os.path.join("C:\\Users\\Desktop\\udi\\pcm-audio") for subdir, dirs, files in os.walk(directory): for file in files: if file.ends

我必须从文件夹中只读取一个.txt文件,所以我正在执行上述操作。后来我试着把我读到的东西打印出来。但是当我运行上面的程序时,我没有得到任何输出。有人能帮我一下吗?有什么错吗

使用以下命令:

directory =os.path.join("C:\\Users\\Desktop\\udi\\pcm-audio") 
for subdir, dirs, files in os.walk(directory): 
    for file in files: 
        if file.endswith(".txt"): 
            f=open(os.path.join(subdir, file),'r') 
            a = f.read() 
            if re.findall('\"status_code\": 0', a):
                print('Valid one') 
            else: 
                print('Invalid') 
        f.close()
将分隔符
/
替换为
\
(Python2)、
\
(Python3)

要读取特定行,可以使用
linecache

...  
f=open(os.path.join(subdir, file),'r')  
...
使用以下命令:

directory =os.path.join("C:\\Users\\Desktop\\udi\\pcm-audio") 
for subdir, dirs, files in os.walk(directory): 
    for file in files: 
        if file.endswith(".txt"): 
            f=open(os.path.join(subdir, file),'r') 
            a = f.read() 
            if re.findall('\"status_code\": 0', a):
                print('Valid one') 
            else: 
                print('Invalid') 
        f.close()
将分隔符
/
替换为
\
(Python2)、
\
(Python3)

要读取特定行,可以使用
linecache

...  
f=open(os.path.join(subdir, file),'r')  
...

泽蒂斯的回答是正确的。 问题出在您给出的包含反斜杠的开始目录路径中

您可以将其更改为正向斜杠/而不是反斜杠\如下所示:

f=open(os.path.join(subdir, file),'r')  
directory = os.path.normpath("C:/Users/sam/Desktop/pcm-audio")
或者可以使用双反斜杠引用它们,如下所示:

f=open(os.path.join(subdir, file),'r')  
directory = os.path.normpath("C:/Users/sam/Desktop/pcm-audio")
也可用于规范化路径。你不需要这里,因为你不参加任何活动

因此,这应该是可行的:

directory = os.path.normpath("C:\\Users\sam\\Desktop\\pcm-audio")

泽蒂斯的回答是正确的。 问题出在您给出的包含反斜杠的开始目录路径中

您可以将其更改为正向斜杠/而不是反斜杠\如下所示:

f=open(os.path.join(subdir, file),'r')  
directory = os.path.normpath("C:/Users/sam/Desktop/pcm-audio")
或者可以使用双反斜杠引用它们,如下所示:

f=open(os.path.join(subdir, file),'r')  
directory = os.path.normpath("C:/Users/sam/Desktop/pcm-audio")
也可用于规范化路径。你不需要这里,因为你不参加任何活动

因此,这应该是可行的:

directory = os.path.normpath("C:\\Users\sam\\Desktop\\pcm-audio")

它不起作用。只是得到一个空屏幕。我在屏幕上看不到任何东西。它正在使用双斜线。泰:)你能告诉我如何从.txt文件中读取特定行吗?如果我在.txt文件中查找一个名为status code==0的单词。如果不同的.txt文件中的行号不同,那么会有很多.txt文件。那么怎么办?@Hemanth Venkatappa您想从文本文件中获取
状态代码==?
?在.txt文件中有一个状态代码行。只是我想检查状态代码==0,然后它是有效的,否则它是无效的。它不工作。只是得到一个空屏幕。我在屏幕上看不到任何东西。它正在使用双斜线。泰:)你能告诉我如何从.txt文件中读取特定行吗?如果我在.txt文件中查找一个名为status code==0的单词。如果不同的.txt文件中的行号不同,那么会有很多.txt文件。那么怎么办?@Hemanth Venkatappa您想从文本文件中获取
状态代码==?
?在.txt文件中有一个状态代码行。我只想检查状态代码==0,然后它是有效的,否则它是无效的。它正在使用双斜杠。谢谢:)您能告诉我如何读取.txt文件中的特定行吗?要读取文件中的行,可以在文件对象上循环。这是一种高效、快速的内存存储方式,并产生了简单的代码:
对于f:print line中的行,
搜索readlinesExample:If.txt文件为200行.txt。如果我想读取.txt文件中状态代码==0的特定行,那么如何读取?。我修改了我的问题。它使用双斜杠。谢谢:)您能告诉我如何读取.txt文件中的特定行吗?要读取文件中的行,可以在文件对象上循环。这是一种高效、快速的内存存储方式,并产生了简单的代码:
对于f:print line中的行,
搜索readlinesExample:If.txt文件为200行.txt。如果我想读取.txt文件中状态代码==0的特定行,那么如何读取?。我修改了我的问题。