即使目录正确,也找不到文件

时间:2015-05-10 21:27:47

标签: python file directory

我目前正在尝试制作游戏,我正在从python文件所在的同一文件夹中导入一个按钮的图像。方法:

<form action="upload.php" method="post" enctype="multipart/form-data">

输出(imagepath)显示为dirname, filename = os.path.split(os.path.abspath(sys.argv[0])) imagepath = os.path.join(dirname, "redvase.png") vase1 = Button(root, relief=FLAT, background="white", image=imagepath) vase1.place(x=330,y=240,height=30,width=30)

我的文件是在那个确切的路径上,但我仍然收到错误,说该文件不存在。

C:\Users\ - - \Desktop\Pythonproject\redvase.png

如果有帮助,我使用的是Windows和Python 3.4.3。

2 个答案:

答案 0 :(得分:0)

按钮的图像属性接受Image对象的输入,而不是图像文件的路径。 为了获得图像,您可以使用PhotoImage:

photo=PhotoImage(file="redvase.gif")
vase1 = Button(root, relief=FLAT, background="white", image=photo)

答案 1 :(得分:0)

我有同样的问题。我创建了目录,但是却说找不到。我的代码如下:

import os
os.mkdir("FireFiles")
os.chdir("Python/FireFiles")
f=open("2015BahiaFires.txt", "w")
f.write("Month /tNumber")
f.close
相关问题