How to write to gzip file from elixir code - gzip

I would like to write gzip file from elixir code.
I tried to following code, but it doesn't work well.
io_device = File.open!("/path/to/file.gzip", [:write, :compressed])
IO.write(io_device, "test")
IO.write returns :ok, but, /path/to/file.gzip is empty.
How can I write to gzip file?

You can also do whole thing in one step:
File.write "/path/to/file.gzip", "test", [:compressed]

You need one more step: close the file so that any buffered data gets written:
File.close io_device

Related

pandas.read_csv of a gzip file within a zipped directory

I would like to use pandas.read_csv to open a gzip file (.asc.gz) within a zipped directory (.zip). Is there an easy way to do this?
This code doesn't work:
csv = pd.read_csv(r'C:\folder.zip\file.asc.gz') // can't find the file
This code does work (however, it requires me to unzip the folder, which I want to avoid because my dataset currently contains thousands of zipped folders):
csv = pd.read_csv(r'C:\folder\file.asc.gz')
Is there an easy way to do this? I have tried using a combination of zipfile.Zipfile and read_csv, but have been unsuccessful (I think partly due to the fact that this is an ascii file as well)
Maybe the followings might help.
df = pd.read_csv('filename.gz', compression='gzip')
OR
import gzip
file=gzip.open('filename.gz','rb')
content=file.read()

How to extract .sql file that seems to be a .zip

I have received a file from a customer. The file is said to be
SQL code (application/sql)
However, this has turned out to be wrong: nothing could open it. It turns out it was secretely a .zip file. By renaming it to '.zip' and manually extracting it I was able to get the files contained in it. I would like to do a similar process in python.
So far I've renamed the file:
file_name_zip = file_name.replace('.sql', '.zip')
os.rename(file_name, file_name_zip)
And I've tried extracting it:
zip_ref = zipfile.ZipFile(file_name_zip, 'r')
zip_ref.extractall(extracted_file)
However, this failed because
zipfile.BadZipFile: File is not a zip file
I've googled, and apparently this can sometimes be fixed using:
zip_file_name_2 = zip_file_name.replace('.zip', '2.zip')
os.system(f'zip -FF {zip_file_name} --out {zip_file_name_2}')
This required me to put in a bunch of settings, which I wasn't able to figure out. There must be a better way to go about this.
Does anybody know how to parse such an .sql file?

Create file with UTF-16LE encoding

I am trying to create a tab separate CSV via kotlin.
For the requirement we need to have UTF-16LE for the produced file encoding.
My stripped down code is something like this:
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStreamWriter
fun main(args: Array<String>) {
val fileOutputStream = FileOutputStream(File("bla.csv"))
val writer = OutputStreamWriter(fileOutputStream, Charsets.UTF_16LE)
writer.write("bla\tbla\tbla")
writer.write("\n")
writer.write("lab\tlab\tlab")
writer.flush()
writer.close()
}
So after executing this program the file generated has this information:
(I am running file on the actual file)
file -I bla.csv
bla.csv: application/octet-stream; charset=binary
This is what I get when I go for
Charsets.UTF_16LE
I have tried using other UTF-16 variation which made me even more confuse!
So if I use Charsets.UTF_16 it will result in:
file -I bla.csv
bla.csv: text/plain; charset=utf-16be
And if I use Charsets.UTF_16BE it will result in:
file -I bla.csv
bla.csv: application/octet-stream; charset=binary
So after a lot of self doubt and being sure that I am doing something wrong I have give up and come here.
Any guidance will be appreciated. Thanks in advance
I suspect this is a limitation of the file command, and not a problem with your code (which is fine*).
If you write a Byte Order Mark (\uFEFF) as the first character of the file, then file recognises it fine:
> file bla.csv
bla.csv: Little-endian UTF-16 Unicode text
> file -I bla.csv
bla.csv: text/plain; charset=utf-16le
The file should be perfectly valid without a BOM, though.  So I'm not sure why file isn't recognising it.  It may be that it's not always possible to safely identify UTF16-LE without a BOM, though you'd think a case like this (where every other byte is 0) would be a safe bet!
(* Well, there are always potential improvements…  For example, it'd be safer to wrap the output in a call to writer.use() instead of closing the file manually.  You could wrap the OutputStreamWriter in a BufferedWriter for efficiency.  And in production code, you'd want some error handling, of course.  But none of that's related to the question!)

Pipe Console App to file from batch file

I have a console app (Visual Studio - VB), it runs, it does it's job.
I also have a batch file that runs the program and everything, but I want the output of my console app to also send to a text file.
This is my current batch, which creates the text file, but nothing is in it.
Start "" "C:\Users\wrossi\Desktop\NetLogOnSysInfo Solution\NetLogOnSysInfo Project\bin\Debug\NetLogOnSysInfo Project.exe" -all >>%ComputerName%.txt
Exit
Not sure if the batch is wrong, or if I should look at my program. Also, how do I define a path so I can put the output file exactly where I want it?
"C:\Users\wrossi\Desktop\
NetLogOnSysInfo Solution\NetLogOnSysInfo Project\bin\Debug\NetLogOnSysInfo Project.exe"
-all > %ComputerName%.txt
No need to use Start.
You are pretty close to what I think you want to do:
c:\console.exe > c:\output.exe
c:\console.exe or wherever the location of your console app > redirect stdout (use >> to append) c:\output.txt or wherever you want to create the text file.
Hope this helped.
Also just in case:
type stdin.txt | console.exe > stdout.txt
In the above example you can redirect an input from stdin.txt and pipe it into console.exe then redirect the output of console.exe to stdout.txt
I found one code recently after searching alot
if u wanna run .bat file in vb or c# or simply
just add this in the same manner in which i have written
here is the code
code > C:\Users\Lenovo\Desktop\output.txt
just replace word "code" with your .bat file code or command and after that the directory of output file

How do I get the .write function to work with PyScripter?

To reproduce problem:
In the PyScripter editor, write:
outf = open('output.txt', 'w')
outf.write('hello, world!')
Result:
For me at least, here is what happens, when output.txt does not already exist.
output.txt is created
output.txt will contain no data or text at all when opened by any text editor.
So my question is, how do I make this work?
Other information:
I am using PyScripter 2.5.3.0 x64, with Python 2.7.3, 64 bit as the interpreter.
Printing to the console works fine, all other functions and code work fine.
When I use python in Command Prompt, I can write to output files fine. My problem is only in PyScripter.
Thanks,
DS
The question was already resolved in comments, but I will post a complete answer regardless.
Writes to files may be delayed. To make sure they aren't, force a flush by closing the file:
outf.close()
If you don't want to explicitly call close, try using with ... as:
with open('output.txt', 'w') as outf:
outf.write('hello, world!')