更好地学习和理解 Python 我想编写一个基于 youtube-dl 的脚本,该脚本下载播放列表并将所有这些 flv 视频移动到特定目录中。
到目前为止,这是我的代码:
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 视频。我该如何过滤?
谢谢!