Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
Create and read a Temporary file in a Django Command

I need to read a flow url as csv then this is what I do:

class Command(BaseCommand):
    help = 'Admin command to import feed'

    def _download_flow(self, url):
        req = requests.get(url, stream=True)

        if req.status_code == 200:
            tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
            for line in req.iter_lines():
                tmp.write(line)
            return tmp
        raise Exception('error:{}'.format(req.status_code))

    def handle(self, *args, **options):
        catalog = self._download_flow(options['url'])
        with open(catalog.name, 'rU') as csvfile:

            reader = csv.DictReader(
                csvfile,
                delimiter=';',
                quotechar='"')

            for row in reader:
                raise Exception(row)

        catalog.close()

Basically, from an url, I create a temporary csv file. Then, now I want to parse this file to work with lines but I don't know why my exception is not raised. (My file has content, i've checked). Do you have any clue to help me ?

Thanks

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The problem came from the _download() method, the correct way to construct the file is:

    def _download_flow(self, url):
        req = requests.get(url, stream=True)

        if req.status_code == 200:
            tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
            for chunk in req.iter_content():
                tmp.write(chunk)
            return tmp
        raise Exception('error:{}'.format(req.status_code))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!