python3 try to handle multiple different zipped pcap files - python

I try to find a solution to work (just read) a PCAP/pcap File inside a ZIP Archive.
I won´t unzip the File to disk, because there are 1000 of zips, and each of them has one pcap inside
e.G.
file1.zip => contains file1.pcap
file2.zip => contains file2.pcap ...
I am using pyshark atm but can also use dpkt,scapy if it works with memory zips. But pyshark try to find the file.
>>> with ZipFile(pcaps_zip, "r") as z_in:
... for pcap in z_in.namelist():
... pyshark.FileCapture(pcap)
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/local/lib/python3.6/site-packages/pyshark/capture/file_capture.py", line 48, in
__init__
+ str(self.input_filename)
FileNotFoundError: [Errno 2] No such file or directory: file1.pcap

Related

Python zipfile: file name with new line characters

Somebody managed somehow to add a new line character \r\n to the name of a file in a zip, and that makes ZipFile fail when it tries to extract the zip:
2019-07-23 14:05:12,285 - __main__ - ERROR - Error desconocido: [Errno 22] Invalid argument: 'descargados\\03_26298_19\\ANEXO\r\n.pdf'. Saliendo.
Traceback (most recent call last):
File "motor.py", line 51, in main
procesar_descarga(zip_object, ruta_temp, ruta_final)
File "C:\Users\david\pycharmProjects\descargueitor2\volcado.py", line 90, in procesar_descarga
zip_object.extractall(str(ruta_temp))
File "C:\Users\david\Anaconda3\lib\zipfile.py", line 1616, in extractall
self._extract_member(zipinfo, path, pwd)
File "C:\Users\david\Anaconda3\lib\zipfile.py", line 1670, in _extract_member
open(targetpath, "wb") as target:
OSError: [Errno 22] Invalid argument: 'descargados\\03_26298_19\\ANEXO\r\n.pdf'
I tried the same file with several programs:
The built-in compressed files reader in Windows explorer just ignores the file: it is not listed nor extracted.
WinZip lists the file, but throws an error when opening or extracting the file.
7Zip can read and extract the file: it just converts the bad characters to underscores.
Is there any way to deal with this in Python? It looks like files in a zip cannot be renamed using the library.

Python get data from named pipe

How do I read from a named pipe in Python 3.5.3?
The pipe's name and location is /tmp/shairport-sync-metadata and it can be viewed by anybody, but only modified by the shairport-sync user.
Other websites and questions say to use os.mkfifo("pipe-location") but I've been getting this error:
>>> import os
>>> os.mkfifo("/tmp/shairport-sync-metadata")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
os.mkfifo("/tmp/shairport-sync-metadata")
FileExistsError: [Errno 17] File exists
Is there a way to get around this? Sorry for the nooby question.
os.mkfifo is used to create fifo. Use open to open/read fifo already exist:
with open('/tmp/shairport-sync-metadata') as f: # add `rb` for binary mode
# line-by-line read
for line in f:
print(line)
# f.read(1024) # to read 1024 characters

Looping over csv files in a Python directory

I'm running this script to loop over a directory of CSV files to run cross validation on.
for filename in os.listdir("/Users/name/PycharmProjects/Project/Data/Nod"):
k_fold(filename)
I get the error:
Traceback (most recent call last):
File "/path_of_cross_validation_file", line 28, in <module>
k_fold(filename)
File "/path_of_cross_validation_file", line 7, in k_fold
data = open(myfile).readlines()
IOError: [Errno 2] No such file or directory: 'file_name.csv'
How do I iterate through all these files to split the data into training and testing files?
For reference, a file in Nod could look like this:
x,y,z
-1.3518261999999999,0.19841946999999999,0.058807577999999999
-1.5427636999999998,0.54079030000000006,-0.15981296
-1.4453497,0.04129998,0.046387657999999998
-1.4743793000000001,-0.064793080000000003,0.18315643000000001
It turns out I needed to use the glob module.
Here's the solution:
for filename in glob.iglob('Path_to_directory/*.csv'):
k_fold(filename)

How do I change/choose the file path in Python?

I'm trying to access a .txt file in Python and I can't figure out how to open the file. I ended up copying the contents into a list directly but I would like to know how to open a file for the future.
If I run this nothing prints. I think it's because Python is looking in the wrong folder/directory but I don't know how to change file paths.
sourcefile = open("CompletedDirectory.txt").read()
print(sourcefile)
The file CompletedDirectory.txt is probably empty.
If Python could not find the file, you would get a FileNotFoundError exception:
>>> sourcefile = open("CompletedDirectory.txt").read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'CompletedDirectory.txt'
Note that using read() in this way is not recommended. You're not closing the file properly. Use a context manager:
with open("CompletedDirectory.txt") as infile:
sourcefile = infile.read()
This will automatically close infile on leaving the with block.
You can get the current working directory:
import os
os.getcwd()
Then just concat it with the file container directory
os.path.join("targetDir", "fileName")

Python BZ2 IOError: invalid data stream

Traceback (most recent call last):
File "TTRC_main.py", line 309, in <module>
updater.start()
File "TTRC_main.py", line 36, in start
newFileData = bz2.BZ2File("C:/Program Files (x86)/Toontown Rewritten/temp/phase_7.mf.bz2"," rb").read()
IOError: invalid data stream
The code to retrieve file I'm getting that's giving me this error is:
newFileComp = urllib.URLopener()
newFileComp.retrieve("http://kcmo-1.download.toontownrewritten.com/content/phase_7.mf.bz2", "C:/Program Files (x86)/Toontown Rewritten/temp/phase_7.mf.bz2")
What do I do to fix this error? Its not really descriptive. (to me)
Could the issue be occuring because of the extra spacein the file mode? -
newFileData = bz2.BZ2File("C:/Program Files (x86)/Toontown Rewritten/temp/phase_7.mf.bz2"," rb").read()
Try this -
newFileData = bz2.BZ2File("C:/Program Files (x86)/Toontown Rewritten/temp/phase_7.mf.bz2","rb").read()
For me the issue was that the files were not in .bz2 format.
Make sure file is bz2 format.
Make sure the read and write actions are the same "r","w" or "rb","wb"
Like Anand said, no space in "rb".

Categories