In [43]:
import pymongo
import datetime
from datetime import date 
import pandas as pd

client = pymongo.MongoClient('localhost', 27017)
db = client['stackoverflow']
jobs = db['jobs']

t = datetime.date.today()
weeknum = t.isocalendar()[1] 
year = t.isocalendar()[0]

In [46]:
def export_jobs(year, weeknum): 
    year_regex = '^' + str(year) 
    df = pd.DataFrame(list(jobs.find({ "weeknum": weeknum, "date" : {"$regex": year_regex}}))) 
    
    # Dropping _id column
    df = df.drop('_id', 1)

    out_path = "./data/export/jobs_export_" + str(year) + "_w" + str(weeknum)  + ".csv"
    print "Exporting " + out_path + " with " + str(len(df.index)) + " rows."
    
    df.to_csv(out_path, index = False, encoding='utf-8')

In [47]:
# this week 
export_jobs(year, weeknum)
# previous week
export_jobs(year, weeknum-1)


Exporting ./data/export/jobs_export_2017_w1.csv with 152 rows.
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-47-3949065087d8> in <module>()
      1 # this week
----> 2 export_jobs(year, weeknum)
      3 # previous week
      4 export_jobs(year, weeknum-1)

<ipython-input-46-8b6f7ec3463b> in export_jobs(year, weeknum)
      9     print "Exporting " + out_path + " with " + str(len(df.index)) + " rows."
     10 
---> 11     df.to_csv(out_path, index = False, encoding='utf-8')
     12 

/usr/local/lib/python2.7/site-packages/pandas/core/frame.pyc in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal, **kwds)
   1342                                      doublequote=doublequote,
   1343                                      escapechar=escapechar, decimal=decimal)
-> 1344         formatter.save()
   1345 
   1346         if path_or_buf is None:

/usr/local/lib/python2.7/site-packages/pandas/formats/format.pyc in save(self)
   1524             f = _get_handle(self.path_or_buf, self.mode,
   1525                             encoding=self.encoding,
-> 1526                             compression=self.compression)
   1527             close = True
   1528 

/usr/local/lib/python2.7/site-packages/pandas/io/common.pyc in _get_handle(path, mode, encoding, compression)
    424                 f = open(path, mode, errors='replace')
    425         else:
--> 426             f = open(path, mode)
    427 
    428     return f

IOError: [Errno 2] No such file or directory: './data/export/jobs_export_2017_w1.csv'

In [ ]: