Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda