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 基于文件名而不是数据类型读取文件_Python_File_Names - Fatal编程技术网

Python 基于文件名而不是数据类型读取文件

Python 基于文件名而不是数据类型读取文件,python,file,names,Python,File,Names,我对python非常陌生。我想根据文件名而不是数据类型读取文件。假设我在文件夹中有Hello.txt_1、Hello.txt_2、Hello.txt_3,这些文件由外部代码自动创建,其中Hello.txt_3是最新的文件。现在,我想阅读最新创建的文件Hello.txt_3并检查其内容。如何在python中完成?我已经找到了具有公共数据类型的文件,但没有找到公共文件名。使用glob执行通配符匹配。下面的示例将找到按您的状态命名的所有文件,last_file将包含创建时最新文件的名称(如果未找到文件

我对python非常陌生。我想根据文件名而不是数据类型读取文件。假设我在文件夹中有Hello.txt_1、Hello.txt_2、Hello.txt_3,这些文件由外部代码自动创建,其中Hello.txt_3是最新的文件。现在,我想阅读最新创建的文件Hello.txt_3并检查其内容。如何在python中完成?我已经找到了具有公共数据类型的文件,但没有找到公共文件名。

使用glob执行通配符匹配。下面的示例将找到按您的状态命名的所有文件,
last_file
将包含创建时最新文件的名称(如果未找到文件,则为
None


PS:这个问题是一个非常初级的问题,通过谷歌搜索应该很容易解决。

根据我对这个问题的理解,你可能想忽略文件类型,因为你不能依靠它们来告诉你你想知道什么。 如果文件类型包含数字/字母数字,“排序”将按相反顺序排序。然后,您可以简单地阅读第一部分:

#!/usr/bin/python
from os import listdir
from os.path import isfile, join

#this is where you declare you directory
directory_2_look = '/var/tmp/lee/'

#This creates a list of the contents of your directory
files = [ f for f in listdir(directory_2_look) if isfile(join(directory_2_look,f)) ]

#this sorts the files for you in reverse order
files = sorted(files, reverse=True)
print files[0]

#this allows you to read the last file and print it out. If you want to write to it simply change the 'r'
file_to_read = open(join(directory_2_look,files[0]), 'r')

print file_to_read.read()
结果将有点像这样:

['script.py'、'file.txt_99'、'file.txt_4'、'file.txt_3', ‘file.txt_22’、‘file.txt_21’、‘file.txt_2’、‘file.txt_1’, 'file.txt_0']script.py

!/usr/bin/python from os import listdir from os.path import isfile,join directory_2_look='/var/tmp/lee/'files=[f代表中的f listdir(directory_2_look)if isfile(join(directory_2_look,f))]打印 已排序(文件,反向=真)文件=已排序(文件,反向=真)打印 文件[0]文件\u到\u读取=打开(加入(目录\u 2\u查找,文件[0]),'r')

将文件\u打印到\u read.read()


os.path.mtime
在这里很有价值。关于如何获取files@squiguy他可能想要
ctime
而不是
mtime
谢谢。当我按照下面的方式编辑代码时,它正确地为我提供了最后创建的文件。import glob import os last_file=None time=0对于glob.glob中的I(“Hello.txt_*”):if os.path.getctime(I)>time:last_file=I time=last_file print last_file@Jon克莱门茨:非常感谢。通过进一步添加这些,我实现了我的目标lines@Jon元素:import glob import os last_file=None time=0对于glob.glob中的i(“Hello.txt_*”):if os.path.getctime(i)>time:last_file=i time=last_file打印last_file f=open(last_file,'r')q=f.read()大量打印qThanks。当我检查这一部分时,我得到了在正在打印和正在打印的文件夹上创建的相同python程序。在您给出的示例中,是否可以仅对file.txt执行此操作,我想您应该将file.txt_99作为输出,而不是script.py或任何其他文件。是的,您只需要在另一个目录中创建脚本:-)或者,查看文件[1]。它给你第二个,因为第一个是你的脚本。然而,这是危险的,假设您在目录call script.py1中创建另一个脚本,您将需要查看文件[2]等等。最好不要在包含数据的目录中创建脚本。我把它放在那里只是因为我想让你看一些内容。所有其他文件都是空的!:-)谢谢:-)但我需要执行确切的操作,但对于仅以特定名称开头的文件,如“file.txt”,因为在我的情况下,还有其他与Hello.txt一起创建的文件,如Error.txt等等。是否可以执行相同的操作,但仅针对我们感兴趣的文件?
#!/usr/bin/python
from os import listdir
from os.path import isfile, join

#this is where you declare you directory
directory_2_look = '/var/tmp/lee/'

#This creates a list of the contents of your directory
files = [ f for f in listdir(directory_2_look) if isfile(join(directory_2_look,f)) ]

#this sorts the files for you in reverse order
files = sorted(files, reverse=True)
print files[0]

#this allows you to read the last file and print it out. If you want to write to it simply change the 'r'
file_to_read = open(join(directory_2_look,files[0]), 'r')

print file_to_read.read()