How can I save sort .txt into files? Python or shell script -


i have python program downloads article text , turns txt file. program spits out txt files in directory program located in. arrange text in folders specific news source came from. save data in folder in python program , change directory news source file changes? or should create shell script runs python program inside folder needs in? or there better way sort these files missing?

here code of python program:

import feedparser goose import goose import urllib2 import codecs  url = "http://rss.cnn.com/rss/cnn_tech.rss"  feed = feedparser.parse(url) g = goose()  entrylength = len(feed['entries']) count = 0  while true:     article = g.extract(feed.entries[count]['link'])     title = article.title     text = article.cleaned_text      file = codecs.open(feed['entries'][count]['title'] + ".txt", 'w', encoding = 'utf-8')     file.write(text)     file.close()      count = count + 1     if count == entrylength:         break 

if give save functions filenames, save current directory. however, if provide them paths, files end there. python takes care of it.

folder = 'whatever' #the folder wish save files in name = 'somefilename.txt' filename = os.path.join(folder, filename) 

using filename make file end in folder 'whatever/'

edit: see you've posted code now. br1ckb0t mentioned in comment below, in code write codecs.open(folder + feed['entries'].... make sure append slash folder if that, or it'll end part of filename.


Comments