Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.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中以微秒级的精度设置文件的mtime_Python_Linux_Stat - Fatal编程技术网

在python中以微秒级的精度设置文件的mtime

在python中以微秒级的精度设置文件的mtime,python,linux,stat,Python,Linux,Stat,假设我创建了一个测试文件并检查其mtime: $ touch testfile.txt $ stat testfile.txt File: `testfile.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fc01h/64513d Inode: 3413533 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1

假设我创建了一个测试文件并检查其mtime:

$ touch testfile.txt 
$ stat testfile.txt
  File: `testfile.txt'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fc01h/64513d    Inode: 3413533     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ me)   Gid: ( 1000/ me)
Access: 2014-09-17 18:38:34.248965866 -0400
Modify: 2014-09-17 18:38:34.248965866 -0400
Change: 2014-09-17 18:38:34.248965866 -0400
 Birth: -

$ date -d '2014-09-17 18:38:34.248965866 -0400' +%s
1410993514
上面的
mtime
以微秒的精度列出(我意识到系统时钟分辨率使得该分辨率的较高部分变得无用)。
utimes(2)
系统调用允许我在几微秒内通过。但是,
os.utime()
函数似乎将其组合为一个数字

我可以像这样传递一个
浮点值

>>> os.utime('testfile.txt', (1410993514.248965866, 1410993514.248965866))
但是现在

$ stat testfile.txt 
  File: `testfile.txt'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fc01h/64513d    Inode: 3413533     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ me)   Gid: ( 1000/ me)
Access: 2014-09-17 18:38:34.248965000 -0400
Modify: 2014-09-17 18:38:34.248965000 -0400
Change: 2014-09-17 18:46:07.544974140 -0400
 Birth: -
大概是因为该值被转换为
浮点值
,python知道最好不要相信最后几个小数位,所以精度会丢失

有没有办法通过python设置完整微秒字段?

您已经在设置完整微秒了。微意味着百万分之一<代码>.248965为248965微秒
.248965866
是248965866纳秒

当然,它也是248965.866微秒,但是Python用于在每个平台上设置时间的可移植API只接受整数微秒,而不接受分数。(事实上,POSIX不要求系统记住任何小于微秒的内容。)

从Python 3.3开始,在支持设置纳秒的系统上添加了一个
ns
关键字参数。1,2因此,您可以传递时间的整数,然后在单独的参数中传递纳秒。像这样:

>>> os.utime('testfile.txt', (1410993514, 1410993514), ns=(248965866, 248965866))

最后一件事:

估计精度会丢失,因为该值已转换为浮点值,python知道最好不要相信最后几位小数

这实际上可能是有道理的……但Python并没有做到这一点。您可以看到它使用的确切代码,但基本上,它们对舍入的唯一补偿是确保负微秒变为0.3

但您是对的,舍入错误在这里是一个潜在的问题…这就是为什么*nix和Python都通过使用单独的
seconds
nanoseconds
整数来避免这个问题(Windows通过使用64位int而不是double来解决这个问题)


1如果您在Unix上,这意味着您有一个类似于
utimes
utimens
函数,但它采用
struct timespec
而不是
struct timeval
。您应该在任何非古老的linux/glibc系统上使用它;在BSD上,这取决于内核,但我认为现在除了OSX以外的所有东西都有它;否则你可能就没有了。但是最简单的检查方法就是
人工智能

2在Windows上,Python使用以100ns为单位的本机Win32 API,因此这样只会额外获得一位数,而不是三位数


3我链接到了3.2,因为3.3有点难以理解,部分原因是您关心的
ns
支持,但主要原因是您不关心的
at
支持。

您正在设置完整的微秒<代码>.248965
为248965微秒
.248965866
是248965866纳米秒。当然它也是
248965.866
微秒。但是Python对每个系统依赖的可移植API(Windows除外)只需要整数微秒。因此,要么使用基于纳秒的API,要么使用基于微秒的API并停止使用纳秒。在UNIX(SVR4/POSIX)和Linux上,
utime
也被限制为微秒。@isedev:Python在POSIX系统上不使用
utime
,它使用
utimes
。而且
utimes
在技术上并不局限于微秒,只是现实生活中的POSIX系统没有一个
struct timeval
,即使允许添加超过秒和微秒的参数。(相反,它们使用单独的
timespec
struct和单独的
foons
函数。哪种Python也可以在3.3+中使用。)好的,重点是,规范确实说“至少包括以下成员”。。。