更好地學習和理解Python我想編寫一個基於youtube-dl的腳本,下載播放列表並將所有flv視頻移動到特定目錄中。Python - 創建目錄並移動特定文件
這是我到目前爲止的代碼:
import shutil
import os
import sys
import subprocess
# Settings
root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/$s'
def download():
files = open('Playlists.txt').readlines()
for playlist in files:
p = playlist.split(';')
# Create the directory for the playlist if it does not exist yet
if not os.path.exists (root_folder % p[0]):
os.makedirs(root_folder % p[0])
# Download every single video from the given playlist
download_videos = subprocess.Popen([sys.executable, 'youtube-dl.py', ['-cit'], [p[1]]])
download_videos.wait()
# Move the video into the playlist folder once it is downloaded
shutil.move('*.flv', root_folder % p[0])
download()
我Playlists.txt的結構如下所示:
Playlist name with spaces;http://www.youtube.com/playlist?list=PLBECF255AE8287C0F&feature=view_all
我遇到兩個問題。首先,字符串格式不起作用。
我得到的錯誤:
Playlist name with spaces
Traceback (most recent call last):
File ".\downloader.py", line 27, in <module>
download()
File ".\downloader.py", line 16, in download
if not os.path.exists (root_folder % p[0]):
TypeError: not all arguments converted during string formatting
誰能給我解釋一下原因嗎?當我打印p [0]時,一切看起來都很好。
其次,我沒有任何線索如何設置正確的shutil.move命令來僅移動剛剛下載的flv視頻。我如何過濾?
謝謝!
我希望你的代碼在你的系統上正確縮進,否則你的'p = playlist.split(';')'行就不會做你想要的。 – 2012-03-15 10:54:13
當然是。我相應地編輯了我的代碼。感謝通知! – orschiro 2012-03-15 12:10:10
編輯到的問題只是提供更多的信息或改進(我的意思是改進格式,固定類型等)。任何其他的改變都會改變這個問題,因此你已經擁有的答案已經沒有意義了。請回滾到以前的[編輯]接受最有幫助的答案。歡迎您打開一個新問題,說明您的新問題:) – 2012-03-15 16:06:40