Найти - Пользователи
Полная версия: Помогите разобраться с папками
Начало » Python для новичков » Помогите разобраться с папками
1
widg
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2
import ast
import re
import os
def get_items_of_post(address):
    url = "https://api.vk.com/method/wall.getById?posts=" + address + "&extended=1&copy_history_depth=2"
    response = urllib2.urlopen(url)
    text = response.read()
    # переводим из текста в словарь
    post_info = ast.literal_eval(text)
    if post_info['response']['wall'] == []:
        return []
    else:
        return post_info['response']['wall'][0]['attachments']
address = raw_input("Enter string of two right numbers of url of post\n")
music = get_items_of_post(address)
for attachment in music:
    type_of_attachment = attachment['type']
    if type_of_attachment != 'audio':
        pass
    else:
        artist = attachment[type_of_attachment]['artist']
        title = attachment[type_of_attachment]['title']
        name_of_file = artist + ' - ' + title + '.mp3'
        url = attachment[type_of_attachment]['url']
        if url == '': # предусмотрен случай, когда аудиозапись удалена из общего доступа
            print name_of_file + " was removed from public access"
            continue
        url = re.sub('\\\\\/', '/', url)  # вместо простых слэшей изначально записываются `\\/`, поэтому нужно их заменить на просто слэши во всей строке
        url = re.sub('\?.*', '', url)   # убираем все символы после расширения `.mp3`, они нам не нужны 
        response = urllib2.urlopen(url)
        if not os.path.exists(artist):
                    os.mkdir(artist)
# вот тут проблема
        dirfile = os.path.join(artist, name_of_file)
        f = open(dirfile, 'w')
        f.write(response.read())
        print name_of_file + " was downloaded" # оповещаем о том, что файл успешно скачался

хочу скачать музыку с стены вк и распихать каждый пост в отдельную папку
ругается:
-59508827_62883
The XX - Intro.mp3 was downloaded
Traceback (most recent call last):
  File "C:/Users/Администратор/Desktop/vkgm.py", line 42, in <module>
    f = open(dirfile, 'w')
IOError: [Errno 2] No such file or directory: 'The XX \\The XX  - Crystalised.mp3'
widg
убрал пробелы
nazv = artist.split()
        if not os.path.exists(nazv):
                    os.mkdir(nazv)
        #dir = os.mkdir(artist)
        dirfile = os.path.join(nazv, name_of_file)
        f = open(dirfile, 'w')

raceback (most recent call last):
File “CUsers/Администратор/Desktop/vkgm.py”, line 39, in <module>
if not os.path.exists(nazv):
File “C:\Python27\lib\genericpath.py”, line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, list found
widg
nazv = str(nazv)
и всё заработало
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB