Python“系统找不到指定的文件”

时间:2013-04-04 22:37:16

标签: python windowserror

我正在尝试为OpenDyslexic字体编写安装程序。我已经在https://github.com/kirbyfan64/OpenDyslexic-Installer有一个Linux版本,但Windows版本(显然)目前尚未完成。这是UAC问题吗?我想不出别的事情要做。有两个奇怪的部分:程序通过else子句以及filecmp找到第一个文件的事实。由于与else问题相同的问题,我感觉filecmp正在执行。

代码:

"""This work is licensed under the
Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported
License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-sa/3.0/
or send a letter to Creative Commons,
444 Castro Street, Suite 900, Mountain View,
California, 94041, USA."""

import urllib
import os
import os.path
import shutil
import filecmp
import sys

def fcheck(filename):
    try:
        with open(filename) as f:
            return True
    except IOError:
        return False
homedir = os.environ['SystemRoot']
tempdir = os.environ['TEMP']
fontsdir = homedir + r'\Fonts'
installcheck = fontsdir + r'\OpenDyslexic-Regular.otf'
dpath = tempdir + r'\OpenDyslexic-Regular.otf'
dpath2 = tempdir + r'\OpenDyslexic-Bold.otf'
dpath3 = tempdir + r'\OpenDyslexic-Italic.otf'
dpath4 = tempdir + r'\OpenDyslexic-BoldItalic.otf'
print 'OpenDyslexic Installer Beta 1'
print ''
raw_input ('Press enter to install OpenDyslexic')
urllib.urlretrieve ('https://github.com/antijingoist/open-dyslexic/raw/master/otf/OpenDyslexic-Regular.otf', dpath)
urllib.urlretrieve ('https://github.com/antijingoist/open-dyslexic/raw/master/otf/OpenDyslexic-Bold.otf', dpath2)
urllib.urlretrieve ('https://github.com/antijingoist/open-dyslexic/raw/master/otf/OpenDyslexic-Italic.otf', dpath3)
urllib.urlretrieve ('https://github.com/antijingoist/open-dyslexic/raw/master/otf/OpenDyslexic-BoldItalic.otf', dpath4)
ifinstall = fcheck(installcheck)
if ifinstall == True:
    updcheck = 0
    originstall = fontsdir + r'\OpenDyslexic-Regular.otf'
    originstall2 = fontsdir + r'\OpenDyslexic-Bold.otf'
    originstall3 = fontsdir + r'\OpenDyslexic-Italic.otf'
    originstall4 = fontsdir + r'\OpenDyslexic-BoldItalic.otf'
    newupdate = filecmp.cmp(dpath, originstall)
    newupdate2 = filecmp.cmp(dpath2, originstall2)
    newupdate3 = filecmp.cmp(dpath3, originstall3)
    newupdate4 = filecmp.cmp(dpath4, originstall4)
    if newupdate == False:
        updcheck = updcheck + 1
    if newupdate2 == False:
        updcheck = updcheck + 1
    if newupdate3 == False:
        updcheck = updcheck + 1
    if newupdate4 == False:
        updcheck = updcheck + 1
    if updcheck != 0:
        raw_input ('Update Avalible. Press Enter to install.')
        os.remove(originstall)
        os.remove(originstall2)
        os.remove(originstall3)
        os.remove(originstall4)
        shutil.copyfile(dpath, originstall)
        shutil.copyfile(dpath2, originstall2)
        shutil.copyfile(dpath3, originstall3)
        shutil.copyfile(dpath4, originstall4)
        tst1 = fcheck(originstall)
        tst2 = fcheck(originstall2)
        tst3 = fcheck(originstall3)
        tst4 = fcheck(originstall4)
        tst5 = 0
        if tst1 == False:
            tst5 = tst5 + 1
        if tst2 == False:
            tst5 = tst5 + 1
        if tst3 == False:
            tst5 = tst5 + 1
        if tst4 == False:
            tst5 = tst5 + 1
        if tst5 != 0:
            if tst1 == True:
                os.remove(originstall)
            if tst2 == True:
                os.remove(originstall2)
            if tst3 == True:
                os.remove(originstall3)
            if tst4 == True:
                os.remove(originstall4)
            raw_input ('Installation Unsuccesful. Press Enter to exit.')
            sys.exit()
        os.remove(dpath)
        os.remove(dpath2)
        os.remove(dpath3)
        os.remove(dpath4)
        raw_input ('Installation Succesful. Press Enter to exit.')
        sys.exit()
    else:
        os.remove(dpath)
        os.remove(dpath2)
        os.remove(dpath3)
        os.remove(dpath4)
        raw_input ('No Update Avalible. Press Enter to exit.')
        sys.exit()
else:
    originstall = homedir + r'\Fonts\OpenDyslexic-Regular.otf'
    originstall2 = homedir + r'\Fonts\OpenDyslexic-Bold.otf'
    originstall3 = homedir + r'\Fonts\OpenDyslexic-Italic.otf'
    originstall4 = homedir + r'\Fonts\OpenDyslexic-BoldItalic.otf'
    newupdate = filecmp.cmp(dpath, originstall)
    newupdate2 = filecmp.cmp(dpath2, originstall2)
    newupdate3 = filecmp.cmp(dpath3, originstall3)
    newupdate4 = filecmp.cmp(dpath4, originstall4)
    shutil.copyfile(dpath, originstall)
    shutil.copyfile(dpath2, originstall2)
    shutil.copyfile(dpath3, originstall3)
    shutil.copyfile(dpath4, originstall4)
    tst1 = fcheck(originstall)
    tst2 = fcheck(originstall2)
    tst3 = fcheck(originstall3)
    tst4 = fcheck(originstall4)
    tst5 = 0
    if tst1 == False:
        tst5 = tst5 + 1
    if tst2 == False:
        tst5 = tst5 + 1
    if tst3 == False:
        tst5 = tst5 + 1
    if tst4 == False:
        tst5 = tst5 + 1
    if tst5 != 0:
        if tst1 == True:
            os.remove(originstall)
        if tst2 == True:
            os.remove(originstall2)
        if tst3 == True:
            os.remove(originstall3)
        if tst4 == True:
            os.remove(originstall4)
        raw_input ('Installation Unsuccesful. Press Enter to exit.')
        sys.exit()
    os.remove(dpath)
    os.remove(dpath2)
    os.remove(dpath3)
    os.remove(dpath4)
    raw_input ('Installation Succesful. Press Enter to exit.')
    sys.exit()

错误:

Traceback (most recent call last):
  File "C:\Users\Ryan\Documents\opendyslexic-installer\opendyslexic_installer.py", line 109, in <module>
    newupdate = filecmp.cmp(dpath, originstall)
  File "C:\Python27\lib\filecmp.py", line 43, in cmp
    s2 = _sig(os.stat(f2))
WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Windows\\Fonts\\OpenDyslexic-Regular.otf'

0 个答案:

没有答案