Domanda

Learning and Comprening Python Meglio Voglio scrivere uno script in base a YouTube-DL che scarica una playlist e sposta tutti quei video FLV in una directory specifica.

Questo è il mio codice finora:

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()
.

La struttura delle mie playlist.txt sembra segue:

Playlist name with spaces;http://www.youtube.com/playlist?list=PLBECF255AE8287C0F&feature=view_all
.

Entrai in due problemi.Prima di tutto la formattazione della stringa non funziona.

ottengo l'errore:

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
.

Qualcuno può spiegarmi la ragione?Quando Stampa P [0] Tutto sembra bene.

Secondo, non ho alcun indizio come impostare il comando di shutol.move corretto per spostare solo il video FLV appena scaricato.Come posso filtrarlo?

Grazie!

È stato utile?

Soluzione

Disclaimer: non sono su Windows

Il punto principale è che dovresti usare os.path.join() per unire percorsi.

Ma sembra esserci un paio di problemi con questa stringa:

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

Penso che:

Quindi direi che è necessario modificare quella linea su:

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

o

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

o

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

E poi fai qualcosa come:

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

Nota: Dal funzionario os.path.join() DOC :

.

Notare che su Windows, poiché è presente una directory corrente per ciascuna unità, os.path.join("c:", "foo") rappresenta un percorso relativo alla directory corrente sull'unità C: (c:foo), non c:\foo.

A giudicare dall'uso Spencer Rathbun Esempio, su Windows dovresti ottenere:

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

Il che significa che è necessario utilizzare uno dei seguenti:

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

Altri suggerimenti

$ Sign non è un carattere valido per la formattazione delle stringhe, utilizzare% invece:

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

Dà: 'TypeError: non tutti gli argomenti convertiti durante la formattazione delle stringhe'

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

Dà: 'C: / Utenti / Robert / Video / YouTube / Playlist / TestFolder'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top