2014-05-07 24 views
-2

有人可以幫我解決我的問題。在Python中使用打印值作爲輸入值

我書面方式小腳本讀取用戶的主目錄,之後

myFile= ?????here must be resul of printing 
winsound.PlaySound(myFile,winsound.SND_NOSTOP) 
+0

是_that_問題? – devnull

+1

你只是在尋找''{} \。myfolder'.format(i)'? – user189

+0

爲什麼你不能這樣做:'myFile ='{} \。myfolder'.format(i); print(myFile); winsound.PlaySound(myFile,...)'? – Bakuriu

回答

1

從該文件夾

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

附加文件夾的名稱和使用文件,而不是手動格式化路徑,使用os.path.join

>>> import os 
>>> path = os.path.join(os.path.expanduser('~'), '.myfolder') # save the value 
>>> print(path)            # print it 
C:\Users\falsetru\.myfolder 
+0

謝謝你的幫助。 os.path.join正是我在尋找的 非常感謝! – user3612886

+1

@ user3612886,歡迎來到Stack Overflow!有些人試圖回答你的問題。如果這對你有幫助,你可以通過[接受答案](http://meta.stackoverflow.com/a/5235)告訴社區,這對你最有用。 – falsetru

1
  1. 路徑不是文件,而是目錄。你不能播放目錄! myFile在路徑末尾需要一個文件名。 你顯然是想找到(說)在指定目錄所有.mp3文件,有一噸的答案對等如何做到這一點:看"Find all files in directory with extension .txt with python"
  2. 你混淆打印一個變量分配它。你想創建一個變量myFile = os.path.join(...whatever...)
+0

感謝您的幫助。 – user3612886