如何通过本地文件名访问本地文件夹? [英] How do I access a local folder through its filename?

查看:125
本文介绍了如何通过本地文件名访问本地文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,它是我的动画/游戏,并且该文件夹中是.pyw文件和.wav音乐文件.我有

I have a folder that is my animation/game, and in the folder is the .pyw file and a .wav music file. I have

import wave
wave.open()

,我不知道在括号中加什么.我知道它应该是文件名,但有人可能会将游戏安装在未知目录中.如何通过文件名访问本地文件夹?

and I don't know what to put in the parenthesis. I know it is supposed to be the filename, but someone might install the game in an unknown directory. How do I access the local folder through the filename?

文件位于同一文件夹中,并且音乐名为"Music.wav".

The files are in the same folder, and the music is named "Music.wav".

推荐答案

常用的方法是使用当前模块的路径(该路径在预定义的__file__变量中自动可用)来确定到子目录中的文件:

A common way this is done is by using the path of the current module, which is automatically available in the predefined__file__variable, to determine the path to the file in the subdirectory:

import os
import wave

mydir = os.path.dirname(__file__)
subdir = 'sounds'
wavefilepath = os.path.join(mydir, subdir, 'Music.wav')
wave.open(wavefilepath)

这篇关于如何通过本地文件名访问本地文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆