What is Python equivalent to Java's FileInputStream? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 8

What is Python equivalent to Java's FileInputStream?

I got this code from the game osu!droid its supposed load the replay file given with its filename. I dont understand much of Java but I can at least convert it, except for one thing which is FileInputStream. I researched on everything in its conversion, even its definition and it doesnt seem to give me the answer. For anyone who knows Java and Python, what is Python equivalent of FileInputStream? Here is the code if you're curious. public boolean load(final String filename) { ObjectInputStream os; try { final ZipInputStream zip = new ZipInputStream(new FileInputStream( new File(filename))); zip.getNextEntry(); os = new ObjectInputStream(zip); // zip.close(); } catch (final Exception e) { Debug.e("Cannot load replay: " + e.getMessage(), e); return false; } //some code that isnt important }

6th Aug 2020, 5:53 AM
Name Omitted until I come back
8 Antworten
+ 4
OfficialAz3[ACTIVEn't]:Rhythm Gamer|Coder|Musician you are not following th correct API for the ZipFile class. In the constructor, it takes 1 parameter - the zip file name (eg. somefile.zip) - and 1 optional parameter - the mode (either 'r' for reading or 'w' for writing). To write to a zipfile, create an object with the mode 'w' zipfileobj = ZipFile("hello.zip", "w") Then you the write method on it which takes 1 parameter- the path of the file as a string (if the file does not exist, you can make one using the normal open() function) zipfileobj.write("image.jpg") To read, create an object with mode "r". Then use the read method on it to read a specific file. The read method returns a normal file object in Python in read mode. zipfile = ZipFile('new.zip', 'r') file = zipfile.read("hello.py") print(file.read()) Read the docs https://docs.python.org/3/library/zipfile.html#zipfile-objects or this https://www.geeksforgeeks.org/working-zip-files-python/
6th Aug 2020, 6:50 AM
XXX
XXX - avatar
+ 1
Use open() which have syntax as f=open(filename) where f is file handler
6th Aug 2020, 6:00 AM
Rajneesh Singh
Rajneesh Singh - avatar
+ 1
Rajneesh Singh open() in Python is File() in java, not FileInputStream. It says it obtains input byte from a file. But will it work if I do open(filename, 'rb')?
6th Aug 2020, 6:02 AM
Name Omitted until I come back
+ 1
OfficialAz3[ACTIVEn't]:Rhythm Gamer|Coder|Musician yes. open(filename, 'rb') will obtain the contents of the file in the form of bytes. In the same way to can write bytes into file by 'wb'
6th Aug 2020, 6:05 AM
XXX
XXX - avatar
0
XXX It catches an error when I do this. from zipfile import ZipFile as zip with open("filename.txt", "rb) as file: unnamedvar = Zipfile(file.read()) is there something wrong with it? It says that bytes dont have an attribute named seek
6th Aug 2020, 6:09 AM
Name Omitted until I come back
0
OfficialAz3[ACTIVEn't]:Rhythm Gamer|Coder|Musician try to use pickle module that may help
6th Aug 2020, 6:19 AM
Rajneesh Singh
Rajneesh Singh - avatar
0
It produces an error if I try to do that directly. But I'll find my way to find it
6th Aug 2020, 6:20 AM
Name Omitted until I come back
0
XXX ahhh thx
6th Aug 2020, 6:52 AM
Name Omitted until I come back