且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过本地文件名访问本地文件夹?

更新时间:2022-04-16 08:48:42

常用的方法是使用当前模块的路径(该路径在预定义的__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)