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

207
Views
Node can't read file saved by python

I am running a python script through a node child_process. This python script downloads a file to disk then returns the filename to node. The problem I'm having is that the file saved by python doesn't appear in my files directory until after the node process closes (by throwing an error since it is trying to read this file saved by python). I am not sure how to get python to "instantly" save the file so my node process can then read it.

Node code:

var sent = false;
const CP = require("child_process");
var process = CP.spawn('python', ["../pyScripts/download.py", vid_url]);
process.stdout.on('data', function(data){
    console.log("recieved data")
    if(sent === false){
        sent = true
        process.kill('SIGINT');
        doStuffWithFile(`${data.toString()}.mp4`);
    }
});

Python code:

#*code that grabs file bytes*
with open(f'{filename}.mp4', "wb") as out:
    out.write(vid_bytes)
print(filename)
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

When you write to a file, don't forget to close it. Try this:

#*code that grabs file bytes*
with open(f'{filename}.mp4', "wb") as out:
    out.write(vid_bytes)
    out.close()
print(filename)
over 4 years ago · Santiago Trujillo Report

0

Wait for exit event, which indicates the Python process is finished.

node.js:

var filename = '';
var sent = false;
var fs = require("fs");
const cp = require("child_process");
var process = cp.spawn('python3', ["/tmp/foo.py", "https://dl8.webmfiles.org/elephants-dream.webm"]);


process.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
  filename = data.toString().trim(); // remove trailing new line!
});

process.on('exit', (code) => {
  console.log(`child process exited with code ${code}`);
  console.log('Reading: ' + filename)

  var stats = fs.statSync(filename)
  var fileSizeInBytes = stats.size;
  var fileSizeInMegabytes = fileSizeInBytes / (1024*1024);
  console.log(`Python downloaded ${fileSizeInMegabytes} MB`)
});

Python:

import time
import requests
import sys
import shutil

filename = '/tmp/test.webm'
headers = {'User-Agent': 'Mozilla 5.0'}
response = requests.get(sys.argv[1], stream=True, headers=headers)
with open(filename, 'wb') as out_file:  # with open(...) closes the file automatically
    shutil.copyfileobj(response.raw, out_file)
print(filename)

Out:

stdout: /tmp/test.webm

child process exited with code 0
Reading: /tmp/test.webm
Python downloaded 8.169429779052734 MB
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!