English 中文(简体)
尽管有文字记录,但文件夹在同一夹中,没有发现错误。
原标题:File not found error despite the script and the texture folder are in the same folder
  • 时间:2023-06-18 20:59:23
  •  标签:
  • python

它报告说,在我加入以下法典时,档案没有发现错误:

background = pygame.image.load( Art/base_vision.jpg )

报告为<代码>。 C:Usersxxx/code。

然而,文字目录为c:/Users/xxx/Desktop/Game/Pyega 测试,该项目中使用的所有艺术均载于c:/Users/xxx/Desktop/Game/Pyega/ 测试/Art。 它不会在“<>条码>/代码”中拿到文档,而是只能使用<条码>C:/Users/xxx中的内容。

这是一个向团队成员派遣Im的团体项目,因此,文字需要在其文件夹中而不是在<代码>C:上使用档案。

问题回答

用途

background = pyega.image.load(>/Art/base_vision.jpg”>

./ will let your program know that it is a relative path and 用途 your current working directory

如果正确读一下你的法典,问题就在于不连贯地使用斜线和斜线。 如果是视窗,请使用:

background = pygame.image.load( Art\base_vision.jpg )

你们需要告诉大家,要走目前文字的道路,并走相对形象的道路。

import os
script_directory = os.path.dirname(__file__)
image_relative_path =  Art\base_vision.jpg 
background = pygame.image.load(os.path.join(script_directory , image_relative_path))

每当你写上<代码>__file__时,如果这一变量存在,它就会永远有文档的绝对路径。

如果你没有具体说明档案的绝对路径<代码> C:users..., 然后,python将查阅与您目前工作名录有关的档案, 现行工作名录

彩虹星座(pathlib) 图书馆处理这一问题。 下面是你描述的范例。 鉴于你描述的双重结构,可以看一看。

Game/
├─ Pygame test/
│  ├─ script.py
│  ├─ Art/
│  │  ├─ art.png
from pathlib import Path

import pygame

SCRIPT_PATH = Path(__file__).resolve() # get the path of the script
PYGAME_PATH = SCRIPT_PATH.parents[0] # parents gives a list of parent directories, get the first one
ART_PATH = PYGAME_PATH / "Art" # the division operator for Path objects concatenates a Path object with a string

background = pygame.image.load(ART_PATH / "art.png")

大部分图书馆将接受“途径”目标,但如果它们不使用,则总是可以通过称为“目标”方法(<条码>)“PATH_OBJECT.as_posix()”的方式将其重新定位。

还指出,在处理道路时,你永远不会使用斜坡。 斜坡在沙捞越时具有特别的意义。 即使在Windows(但使用“路径物体”与你将获得的相片相当,并且也提供了方便之).)上,甲壳就足以正确解释前方的闪).。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签