-2

Can somebody help me in my problem,please.

I am writting small script that reads user home directory,append name of folder and using file from that folder

import os
i=os.path.expanduser('~')
print ('{}\.myfolder'.format (i))

after that

myFile= ?????here must be resul of printing 
winsound.PlaySound(myFile,winsound.SND_NOSTOP)
  • Is _that_ the question? – devnull May 07 '14 at 15:50
  • 1
    Are you just looking for `'{}\.myfolder'.format (i)`? – user189 May 07 '14 at 15:51
  • Why can't you do: `myFile = '{}\.myfolder'.format(i);print(myFile);winsound.PlaySound(myFile, ...)`? – Bakuriu May 07 '14 at 15:51
  • 1
    1) You're confusing *printing* a variable with *assigning* it. You want to create a variable myFile. 2) A path is not a file, it's a directory. You can't play a directory. *myFile* needs a filename at the end of the path. – smci May 07 '14 at 15:57
  • 3) You apparently want to find (say) all .mp3 files in a given directory, there are a ton of answers on SO on how to do that: see ["Find all files in directory with extension .txt with python"](http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python) or other questions. – smci May 07 '14 at 16:06

2 Answers2

1

Instead of manually format the path, use os.path.join:

>>> import os
>>> path = os.path.join(os.path.expanduser('~'), '.myfolder') # save the value
>>> print(path)                                               # print it
C:\Users\falsetru\.myfolder
falsetru
  • 357,413
  • 63
  • 732
  • 636
1
  1. A path is not a file, it's a directory. You can't play a directory! myFile needs a filename at the end of the path. You apparently want to find (say) all .mp3 files in a given directory, there are a ton of answers on SO on how to do that: see "Find all files in directory with extension .txt with python"
  2. You're confusing printing a variable with assigning it. You want to create a variable myFile = os.path.join(...whatever...)
Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146