Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda