2012-03-15 45 views
2

更好地學習和理解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視頻。我如何過濾?

謝謝!

+0

我希望你的代碼在你的系統上正確縮進,否則你的'p = playlist.split(';')'行就不會做你想要的。 – 2012-03-15 10:54:13

+0

當然是。我相應地編輯了我的代碼。感謝通知! – orschiro 2012-03-15 12:10:10

+0

編輯到的問題只是提供更多的信息或改進(我的意思是改進格式,固定類型等)。任何其他的改變都會改變這個問題,因此你已經擁有的答案已經沒有意義了。請回滾到以前的[編輯]接受最有幫助的答案。歡迎您打開一個新問題,說明您的新問題:) – 2012-03-15 16:06:40

回答

4

聲明:我不在windows上

重點是您應該使用os.path.join()加入路徑。

但似乎是一對夫婦與此字符串的問題:

root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/$s' 

我認爲:

  • 你需要使用雙逃脫backslahses。
  • 你的意思是%s而不是$s
  • 無論如何%s,os.path.join()是加入路徑的跨平臺方式。
  • [可選] imho backsleshes更具可讀性。

所以我說,你需要到該行更改爲:

root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists' 

root_folder = 'C:\\Users\\Robert\\Videos\\YouTube\\Playlists' 

root_folder = r'C:\Users\Robert\Videos\YouTube\Playlists' 

然後像做:

my_path = os.path.join(root_folder, p[0]) 
if not os.path.exists(my_path): 
    # ... 

注:從官方os.path.join() doc

Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo .

由有用Spencer Rathbun例子來看,在Windows上,你應該得到:

>>> os.path.join('C', 'users') 
'C\\users' 
>>> os.path.join('C:','users') 
'C:users' 

這意味着你必須使用一個如下:

>>> os.path.join('C:/', 'users') 
'C:\\users' 
>>> os.path.join(r'C:\', 'users') 
'C:\\users' 
+1

python在windows路徑中正向斜槓沒有問題,當你使用基於os的命令(例如shutil.move)時它會自動轉換它們。 – aquavitae 2012-03-15 11:41:54

+0

@aquavitae:很高興知道,我不確定這一點。這是比其他方式更多使用/使用的方式嗎? – 2012-03-15 11:48:58

+0

我不確定。我傾向於使用正斜槓來保持與linux的兼容性(儘可能考慮驅動器盤符)。當我使用反斜槓時,我通常使用一個原始字符串來避免轉義它們。 – aquavitae 2012-03-15 11:56:29

2

$符號不是字符串格式化一個有效的字符,用%來代替:

root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/$s' 
print root_folder % 'testfolder' 

給我: '類型錯誤:不串中轉換的所有參數格式'

root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/%s' 
print root_folder % 'testfolder' 

給我: 'C:/ Users/Robert/Videos/YouTube/Playlists/testfolder'

+1

多麼愚蠢。我完全忽略了它是$而不是%符號...... – orschiro 2012-03-15 12:10:45

相關問題