1

so I recently tried using playsound and the .mp3 is in the same folder as the .py, and it says it could not find the file.

I printed out the current directory and it says it's at C:\Users\me

Shouldn't this be at the same directory as where the .py script is at? This has been happening with my other python scripts where I have to explicitly give it the directory, whereas before I didn't have to and it was just where the python script was at.

Is there a setting for this?

asked Nov 4, 2021 at 22:38

2 Answers 2

1

The current directory is defined by where you execute the python file not the location of it. If you want to change the directory you can use the os module:

import os
os.chdir(PATH)
answered Nov 4, 2021 at 22:44
Sign up to request clarification or add additional context in comments.

Comments

0

If you're looking for a file in the same directory as the executing python program:

basename = os.path.basename(__file__)
my_file_name = basename + "/myfile"
with open(my_file_name) as my_file:
 ...

The variable __file__ contains the full pathname of the currently executing Python program.

answered Nov 4, 2021 at 23:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.