Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 IOError“;没有此类文件或文件夹“;即使文件存在_Python - Fatal编程技术网

Python IOError“;没有此类文件或文件夹“;即使文件存在

Python IOError“;没有此类文件或文件夹“;即使文件存在,python,Python,我在Python2.6.2中编写了一个脚本,用于扫描目录中的SVG,如果SVG太大,则调整其大小。我在我的家用计算机(Vista,Python2.6.2)上写了这篇文章,并处理了一些文件夹,没有任何问题。今天,我在我的工作计算机(XP SP2,Python 2.6.2)上尝试了这个方法,我得到了每个文件的IOErrors,即使文件在目录中。我想我什么都试过了,不知道该怎么办。我是个初学者,所以这可能很简单。任何帮助都将不胜感激 import xml.etree.ElementTree as ET

我在Python2.6.2中编写了一个脚本,用于扫描目录中的SVG,如果SVG太大,则调整其大小。我在我的家用计算机(Vista,Python2.6.2)上写了这篇文章,并处理了一些文件夹,没有任何问题。今天,我在我的工作计算机(XP SP2,Python 2.6.2)上尝试了这个方法,我得到了每个文件的IOErrors,即使文件在目录中。我想我什么都试过了,不知道该怎么办。我是个初学者,所以这可能很简单。任何帮助都将不胜感激

import xml.etree.ElementTree as ET
import os
import tkFileDialog

#--------------------------------------
#~~~variables
#--------------------------------------
max_height = 500
max_width = 428
extList = ["svg"]
proc_count = 0
resize_count = 0

#--------------------------------------
#~~~functions
#--------------------------------------
def landscape_or_portrait():
    resize_count +=1
    if float(main_width_old)/float(main_height_old) >= 1.0:
        print "picture is landscape"
        resize_width()  
    else:
        print "picture is not landscape"
        resize_height()
    return

def resize_height():
    print "picture too tall"
    #calculate viewBox and height
    viewBox_height_new = max_height
    scaleFactor = (float(main_height_old) - max_height)/max_height
    viewBox_width_new = float(main_width_old) * scaleFactor
    #calculate main width and height
    main_height_new = str(viewBox_height_new) + "px"
    main_width_new = str(viewBox_width_new) + "px"
    viewBox = "0 0 " + str(viewBox_width_new) + " " + str(viewBox_height_new)
    inputFile = file(tfile, 'r')
    data = inputFile.read()
    inputFile.close()
    data = data.replace(str(tmain_height_old), str(main_height_new))
    data = data.replace(str(tmain_width_old), str(main_width_new))
    #data = data.replace(str(tviewBox), str(viewBox))
    outputFile = file(tfile, 'w')
    outputFile.write(data)
    outputFile.close()
    return

def resize_width():
    print "picture too wide"
    #calculate viewBox width and height
    viewBox_width_new = max_width
    scaleFactor = (float(main_width_old) - max_width)/max_width
    viewBox_height_new = float(main_height_old) * scaleFactor
    #calculate main width and height
    main_height_new = str(viewBox_height_new) + "px"
    main_width_new = str(viewBox_width_new) + "px"
    viewBox = "0 0 " + str(viewBox_width_new) + " " + str(viewBox_height_new)
    inputFile = file(tfile, 'r')
    data = inputFile.read()
    inputFile.close()
    data = data.replace(str(tmain_height_old), str(main_height_new))
    data = data.replace(str(tmain_width_old), str(main_width_new))
    #data = data.replace(str(tviewBox), str(viewBox))
    outputFile = file(tfile, 'w')
    outputFile.write(data)
    outputFile.close()
    return

#--------------------------------------
#~~~operations
#--------------------------------------
path = tkFileDialog.askdirectory()

for tfile in os.listdir(path):
    #print tfile
    t2file = tfile
    if tfile.find(".") >= 0:
        try :
            if t2file.split(".")[1] in extList:
                print "now processing " + tfile
                tree = ET.parse(tfile)
                proc_count+=1

                # Get the values of the root(svg) attributes
                root = tree.getroot()
                tmain_height_old = root.get("height")
                tmain_width_old =  root.get("width")
                tviewBox  = root.get("viewBox")

                #clean up variables, remove px for float conversion
                main_height_old = tmain_height_old.replace("px", "", 1)
                main_width_old = tmain_width_old.replace("px", "", 1)

                #check if they are too large
                if float(main_height_old) > max_height or float(main_width_old) > max_width:
                    landscape_or_portrait()
        except Exception,e:
            print e

也许是安全问题?可能您没有在文件夹中创建文件的权限

在我看来,您缺少一个
os.path.join(path,tfile)
来获取要打开的文件的完整路径。目前,它应该只适用于当前目录中的文件。

如果您不使用“除此之外的尝试”,而只是向我们展示了导致错误的原因,那么它会更有用。成功!!谢谢你,先生!你是一位绅士和学者。