I have this python code:
import sys
import dpkt
f = file("pcaop.Pcap")
pcap = dpkt.pcap.Reader(f)
i = 0
for ts, buf in pcap:
print "Ya"
dpkt throws NeedData on the 52nd packet. The same one every time - I've checked packet 52 and it is the same as everyone else on wireshark.
What causes this?
Solution is provided here: Python stops reading file using read
I had the same problem when dpkt.pcap was working fine under Linux but failed instantly when run in Windows.
The problem is that when a file is opened in text mode open("filename", "r") the file is read until EOF is encountered. Thus, open("filename", "rb")
Related
I'm executing these lines of code on python terminal and it is working but when i execute it on webserver which is xampp i get output 255 in transcribe_text.txt file but when i run through terminal i get audio to text proper conversion. I'm working on windows environment.
import subprocess
out=subprocess.getstatusoutput(['deepspeech', '--model', 'C:/deepspeech/deepspeech-0.6.0-models/output_graph.pb', '--lm', 'C:/deepspeech/deepspeech-0.6.0-models/lm.binary', '--trie', 'C:/deepspeech/deepspeech-0.6.0-models/trie', '--audio', 'C:/deepspeech/audio/8455-210777-0068.wav'])
#open and read the file after the appending:
print(str(out))
f = open("transcribe_text.txt", "a")
f.write(str(out))
f.close()
#open and read the file after the appending:
f = open("transcribe_text.txt", "r")
print(f.read())
Any solution Please, Thanks
I'm trying to call a python script through incron:
/data/alucard-ops/drop IN_CLOSE_WRITE /data/alucard-ops/util/test.py $#/$#
but I cant seem to read from the file passed. Here is the script:
#!/usr/bin/env /usr/bin/python3
import os,sys
logfile = '/data/alucard-ops/log/'
log = open(logfile + 'test.log', 'a')
log.write(sys.argv[1] + "\n")
log.write(str(os.path.exists(sys.argv[1])) + "\n")
datafile = open(sys.argv[1], 'r')
log.write('Open\n')
data = datafile.readlines()
log.write("read\n")
datafile.close()
The output generated by the script:
/data/alucard-ops/drop/nsco-20180219.csv
True
Open
It seems to stop at the readlines() call. I dont see any errors in the syslog.
Update: It seems that i can use a subprocess to cat the file and it retrieves the contents. But, when i decode it, data.decode('utf-8') I'm back to nothing in the variable.
I ended up using watchdog instead.
Okay, I've been stuck on this one for hours which should have only taken a few minutes of work.
I have the following code which pulls a gzipped CSV file from a datastore:
from ftplib import FTP_TLS
import gzip
import csv
ftps = FTP_TLS('waws-prod.net')
ftps.login(user='foo', passwd='bar')
resp = ftps.retrbinary('RETR data/WFSIV0606201701.700.csv.gz', gzip.open('WFSIV0606201701.700.csv.gz', 'wb').write)
The file appears in the pwd, and I can even open my Mac Decompression tool, and the original CSV is decompressed perfectly.
However, if I try to decompress this file in using the gzip Library, i can't get a UTF8 encoded string to parse:
f=gzip.GzipFile('WFSIV0606201701.700.csv.gz', 'rb')
s = f.read()
I get what appears to be UTF8 bytestrings, however utf8 decoder can't parse the string.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
BUT! If i download directly from the SFTP server using FileZilla, and i do run the gzip.GzipFile code above, it reads it perfectly. Something must be wrong with my downloader/reader but i haven't a clue as to what could be wrong.
resp = ftps.retrbinary('RETR data/WFSIV0606201701.700.csv.gz', gzip.open('WFSIV0606201701.700.csv.gz', 'wb').write)
This line downloads a compressed file, and then compresses it again when writing it to disk.
Replace gzip.open(...).write with open(...).write to write the compressed file directly.
I want to read data from Arduino and store in a text file in pc through serial port using pyserial whenever I try to execute the Python code it gives this message I tried many things but didn't work out.
Code:
import io
import serial
from datetime import datetime
from serial import SerialException
connected=False
outfile='C:\Users\Yassine\hello.txt'
ser = serial.Serial(port="COM12", baudrate=9600,timeout=None,bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1), encoding='ascii', newline ='\r')
with open(outfile,'a') as f:
while ser.isOpen():
try:
datastring=ser.readline()
except serial.SerialException:
pass
print datastring
print datetime.now()
f.write(datetime.now().isoformat() +'\t'+ datastring +'\n' )
f.flush()
while not ser.isOpen():
pass
ser.close()
Check that COM12 is actually the Arduino by looking in device manager.
Or you can execute this in command line to get a list of available serial ports:
python -m serial.tools.list_ports
You may also have something else trying to access the Arduino serial port. Make sure the Serial Monitor in Arduino IDE is closed.
I want to open txt file read the last value of it & write it in other txt file,it's working but the reading value(arg) written to the next file jump the line which is not good for me i want it at the same line with other variables
with open(outfile,'a') as f:
with open (inputfile,'r') as f1:
arg =f1.readline() // that variable i read from the txt
print (arg )
f.write(datetime.now().strftime("%Y-%m-%d ; %H:%M:%S")+'\n'+valueRead+ '\n' +arg+ '\n') // the file i write to
f.flush()
f1.close()
f.close()
TTTs (this is my arg variable that i read from txt file this is what i get)
2017-05-12 ; 15:48:23 TTS (this is how i want it )
Thanks for helping guys
I have a text file with data such as
b'\x00\x09\x00\xfe'
This was piped into a text file from a TCP socket stream. Call this text file 'stream.txt'. I opened this file with the following code:
f = open("stream.txt", "rb")
bytes_read = f.read()
When I open this file within another Python script, I get a '\' for each '\' in the original file. On top of this, I cannot access the bytes array as such, since it appears to have become a string. That is, 'bytes_read' is now
'b"\\x00\\x09\\x00\\xfe"'
How can I recover this string as a bytes array?
The client code I used to capture this data is the following script:
from socket import *
clientsock = socket(AF_INET, SOCK_STREAM)
clientsock.connect(('1.2.3.4', 2000)) # Open the TCP socket
clientsock.sendall(b'myCommand') # Send a command to the server
data = clientsock.recv(16) # Wait for the response
print(data) # For piping to 'stream.txt'
clientsock.close()
As the data was printed to the terminal, I redirected it to a file:
$ python3 client.py > stream.txt
My goal is to bypass the redirect into text file and pipe directly into a plotter... But first I wanted to get this to work.
Was able to solve this by writing directly to a file. So rather than using 'print(data)', and redirecting to a file, I tried this:
file = open("rawData", "wb")
...
file.write(data)
...
file.close()
Was able to process "rawData" as expected.