V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bfqymmt
V2EX  ›  Python

pyinstaller 打包 exe 后,运行异常

  •  
  •   bfqymmt · 2019-12-02 18:49:02 +08:00 · 3100 次点击
    这是一个创建于 1578 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下,在命令模式下运行 python demo.py 是是可以修改文件时间的。

    采用:pyinstaller -F demo.py 打包 exe 后,无法修改文件时间。这会是什么问题呢?

    from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle
    from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING
    from pywintypes import Time
    import time
     
    def modifyFileTime(filePath,createTime,modifyTime,accessTime,offset):
        """
        用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02
        :param filePath: 文件路径名
        :param createTime: 创建时间
        :param modifyTime: 修改时间
        :param accessTime: 访问时间
        :param offset: 时间偏移的秒数,tuple 格式,顺序和参数时间对应
        """
        try:
            format = "%Y-%m-%d %H:%M:%S" #时间格式
            cTime_t = timeOffsetAndStruct(createTime,format,offset[0])
            mTime_t = timeOffsetAndStruct(modifyTime,format,offset[1])
            aTime_t = timeOffsetAndStruct(accessTime,format,offset[2])
     
            fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
            createTimes, accessTimes, modifyTimes = GetFileTime(fh)
     
            createTimes = Time(time.mktime(cTime_t))
            accessTimes = Time(time.mktime(aTime_t))
            modifyTimes = Time(time.mktime(mTime_t))
            SetFileTime(fh, createTimes, accessTimes, modifyTimes)
            CloseHandle(fh)
            return 0
        except:
            return 1
    def timeOffsetAndStruct(times,format,offset):
        return time.localtime(time.mktime(time.strptime(times, format)) + offset)
     
    #调用
    cTime = "2019-02-02 00:01:02" # 创建时间
    mTime = "2019-02-02 00:01:03" # 修改时间
    aTime = "2019-02-02 00:01:04" # 访问时间
     
    fName = r"E:\test.xlsx" #文件路径
    offset = (0,1,2)  # 偏移的秒数
    r=modifyFileTime(fName,cTime,mTime,aTime,offset)
    if r==0:print('修改完成')
    if r==1:print('修改失败')
    
    
    7 条回复    2019-12-03 11:31:51 +08:00
    jdhao
        1
    jdhao  
       2019-12-02 19:02:28 +08:00 via Android
    多设几个 debug 输出看一下?
    yonglanyouyou
        2
    yonglanyouyou  
       2019-12-02 19:49:30 +08:00
    pyinstaller --hiddenimport win32timezone -F demo.py
    UnknownR
        3
    UnknownR  
       2019-12-02 20:11:37 +08:00 via iPad
    还需要更多的 log 才能确定问题。

    要是跑在 windows 下可以用 powershell 写,比如[System.IO]这个类,Methods 里就包含了你需要的三个 time,直接有 set 的方法

    try{
    [System.IO]::SetCreationTime([String]<File PATH>, $cTime)
    [System.IO]::SetLastWriteTime([String]<File PATH>, $mTime)
    [System.IO]::SetAccessTime([String]<File PATH>, $aTime)
    }
    catch{
    throw
    }

    https://docs.microsoft.com/en-us/dotnet/api/system.io.file.setlastwritetime?
    1462326016
        4
    1462326016  
       2019-12-03 08:55:54 +08:00
    管理员试下?
    Marsss
        5
    Marsss  
       2019-12-03 09:12:03 +08:00
    你要知道问题出在哪里,那就把日志导出来看看
    try:
    ......
    except Exception as e:
    with open('log.txt', 'a') as f:
    f.write(str(e))
    zcfnc
        6
    zcfnc  
       2019-12-03 09:56:45 +08:00
    遇事不决看日志
    bfqymmt
        7
    bfqymmt  
    OP
       2019-12-03 11:31:51 +08:00
    @yonglanyouyou 可以了。太感谢你了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   943 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:32 · PVG 05:32 · LAX 14:32 · JFK 17:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.