pyinstaller 打包 exe 后,运行异常 - V2EX
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 3706 次点击
    这是一个创建于 2147 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下,在命令模式下运行 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     Solana     1317 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 17:16 PVG 01:16 LAX 10:16 JFK 13:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86