[英]Can we get the timestamp information with os.listdir in Python (like ls -l)?
I contact an SFTP
server and show files based on the modified timestamp. 我与SFTP
服务器联系,并根据修改后的时间戳显示文件。
Currently, it is done using something like: 目前,它是使用以下方法完成的:
files = os.listdir(SFTP)
files
and get the timestamp using os.stat
. 循环files
并使用os.stat
获取时间戳。 This looping in Step 2 is very costly when the SFTP is on a different server because it has to make a network call from the server to the SFTP for each and every file. 当SFTP在不同的服务器上时,步骤2中的循环非常昂贵,因为它必须为每个文件从服务器向SFTP进行网络调用。
Is there a way to get both the file and modified time using os.listdir
or a similar API? 有没有办法使用os.listdir
或类似的API来获取文件和修改的时间?
I am using a Windows back-end and the SFTP connection usually is done using the win32wnet.WNetAddConnection2 package. 我正在使用Windows后端,通常使用win32wnet.WNetAddConnection2程序包完成SFTP连接。 A generic solution would be helpful, if not a specific solution should be fine too. 通用解决方案将很有帮助,即使不是特定解决方案也可以。
If youre able to send one line commands to the server, you could do [os.stat(i) for i in os.listdir()]
如果您能够将一行命令发送到服务器,则可以[os.stat(i) for i in os.listdir()]
If that doesn't work for you, I suppose you could just do os.system("ls -l")
如果那对您不起作用,我想您可以做os.system("ls -l")
If neither of those work, please do tell me! 如果这些都不起作用,请告诉我!
If you're using Windows, you've got a lot to gain to use os.scandir()
(python 3.5+) or the backport scandir
module: scandir.scandir()
如果您使用的是Windows,那么使用os.scandir()
(python 3.5+)或backport scandir
模块会获得很多收益: scandir.scandir()
That's because on Windows (as opposed to Linux/Unix), os.listdir()
already performs a file stat behind the scenes but the result is discarded except for the name. 这是因为在Windows(相对于Linux / Unix)上, os.listdir()
已经在后台执行了文件状态统计,但结果被丢弃,除了名称之外。 Which forces you to perform another stat
call. 这会迫使您执行另一个stat
调用。
scandir
returns a list of directory entries, not names. scandir
返回目录条目列表,而不是名称。 On windows, the size/object type fields are already filled, so when you perform a stat
on the entry (as shown in the example below), it's at zero cost: 在Windows上,大小/对象类型字段已经填充,因此在条目上执行stat
(如下例所示)时,费用为零:
(taken from https://www.python.org/dev/peps/pep-0471/ ) (取自https://www.python.org/dev/peps/pep-0471/ )
def get_tree_size(path):
"""Return total size of files in given path and subdirs."""
total = 0
for entry in os.scandir(path):
if entry.is_dir(follow_symlinks=False):
total += get_tree_size(entry.path)
else:
total += entry.stat(follow_symlinks=False).st_size
return total
so just replace your first os.listdir()
call by os.scandir()
and you'll have all the information for the same cost as a simple os.listdir()
因此,只需将您的第一个os.listdir()
调用替换为os.scandir()
,您将以与简单os.listdir()
相同的成本获得所有信息。
(this is the most interesting on Windows, and a lot less on Linux. I've used it on a slow filesystem on Windows and got a 8x performance gain compared to good old os.listdir
followed by os.path.isdir
in my case) (这在Windows上是最有趣的,而在Linux上则更少。我在Windows上的慢速文件系统上使用它,与旧版os.listdir
和os.path.isdir
相比,性能提高了8倍。 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.