1

この質問は、さらに下の新しい情報で編集されています。


Cygwin を使用して Windows 7 マシンを使用しています。Python 2.6 と 3.1 の両方がインストールされています。

短い python スクリプトを使用して、以下を確認できます。統計を使用した作成時間、変更時間、アクセス時間、および作成時間。

しかし、問題は.. Windows 7 のファイル プロパティでは、Created Time が11/12/2010 11:57:54 AMと表示されます。

私の質問は: Python スクリプトで Windows Created Time を返すにはどうすればよいですか。

fctime繰り返しますが、以下のスクリプトで返されるを見たくありません。Windows Created Time とは異なります。

これを行う方法を教えてください..なぜ違いがあるのですか..説明してください.

はい.. os.stat.. のドキュメントを読みました。

st_ctime (プラットフォームに依存。Unix での最新のメタデータ変更時刻、または Windows での作成時刻):

$ python /tmp/python/filemodified.py marksix.py
marksix.py
fctime: 11/12/2010 22:58:25
fmtime: 11/12/2010 22:57:01 (windows shows 22:57:01 ok)
fatime: 11/12/2010 22:45:21 (windows shows 22:45:21 ok)
fctimestat: Sat Dec 11 22:58:25 2010 (same as above fctime)
fsize: 1765

この投稿に関連するもの: Mac で Python を使用してファイル作成時間を取得する

ここに私の完全なスクリプトがあります:

import sys
import os
import time

for f in sys.argv[1:]:
    if os.path.exists(f):

        fname = f
        fctime = time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getctime(fname)))
        fmtime =  time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getmtime(fname)))
        fatime =  time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getatime(fname)))
        fsize = os.path.getsize(fname)
        #print "size = %0.1f kb" % float(fsize/1000.0)
        fctimestat = time.ctime(os.stat(fname).st_ctime)

        print fname + '\nfctime: ' + fctime + '\nfmtime: ' + fmtime + '\nfatime: ' + fatime + '\n',
        print 'fctimestat: ' + fctimestat + '\n',
        print 'fsize:', fsize,
        print

追加情報:

今..私は自分の作業環境にいる..以前と同じファイルを使用することはできません..とにかく、cygwin python(2.6.5)とwindows python(2.6.6)でテストし、結果以下に示すように、異なります。

1 つ目は cygwin Python.. で、2 つ目は Windows Python です。そして、Windows Python はファイルのプロパティと一致します... では、この違いは正常ですか..? cygwin python が Windows と同じ日付のセットを取得する必要があるのはなぜですか?

User@COMP /tmp/pythonscr
$ python file_time.py ../testjpg.gif
../testjpg.gif
fctime: 05/01/2011 10:25:52
fmtime: 05/01/2011 10:25:52
fatime: 01/12/2010 17:30:16
fctimestat: Wed Jan  5 10:25:52 2011
fsize: 1536
------


User@COMP /tmp/pythonscr
$ /cygdrive/c/Python26/python.exe file_time.py ../testjpg.gif
../testjpg.gif
fctime: 01/12/2010 17:30:16
fmtime: 05/01/2011 10:25:52
fatime: 01/12/2010 17:30:16
fctimestat: Wed Dec 01 17:30:16 2010
fsize: 1536
------
4

1 に答える 1

1

自宅でもう一度これを試してみました。1)cygwin python 2.6と2)windowspython3.1を同じファイルで使用します。ntpathとos.pathの両方を使用します。

結論は、.. windowspython3.1の結果は3回すべてWindowsのプロパティと一致するということです。

cygwin pythonの場合、作成された時間はWindowsのプロパティと一致しません。ntpathとos.pathの両方を使用すると...他の時間は問題なく一致します。

結果は次のとおりです。

CygwinPython2.6.5の結果

os.path

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getctime(fname)))
'11/12/2010 22:58:25'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getatime(fname)))
'11/12/2010 22:45:21'

ntpath

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getctime(fname)))
'11/12/2010 22:58:25'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getatime(fname)))
'11/12/2010 22:45:21'

WindowsPython3.1の結果

os.path

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getctime(fname)))
'11/12/2010 11:57:54'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(os.path.getatime(fname)))
'11/12/2010 22:45:21'

ntpath

>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getctime(fname)))
'11/12/2010 11:57:54'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getmtime(fname)))
'11/12/2010 22:57:01'
>>> time.strftime("%d/%m/%Y %H:%M:%S",time.localtime(ntpath.getatime(fname)))
'11/12/2010 22:45:21'
于 2011-01-05T14:09:56.853 に答える