Openin exe binary files and editing - python

I'm working on a project to automatize a process. My task is to open an .exe file and edit the binary. I was researching for a possible solution to this task without any success. Does anyone know if there is a Python library or java class that can help? Or any other solution to edit an exe file.

If you just need to edit the binary data contained in the file, then it is just a matter of opening the file as binary and seeking/reading/writing as you would any other binary file.
See the Python docs about reading and writing files: Reading and Writing Files
You'll do something like:
f = open('filename.exe', 'r+b') //'r+b' means read and write binary
Then proceed to seek through the file, read and write where needed.

Depending on your needs, you could treat the .exe file as an "ordinary" binary file as suggested in an other answer.
In other hand, if you need to "decode" Windows portable executable files (accessing header, copying sections), there are some dedicated Python module specialized in that task. I don't know which work the best or has the most features, but you should take a look for example to:
pefile
PELP

Related

Python exclusive write access to files, but other program can read the file

I'm trying to implement a Python script that can write files exclusively, no other process are allowed to write into it aside the Python script itself. But other processes can read it. Is this possible with Python?
It seems that you want to "lock" the file. I think that the locking behavior is different in different operating systems, but at least it will disallow writing the file.
Usually in Windows Python just opening the file to write locks the file (from changing, deleting, etc.). In linux the behavior can be different.
Have a look at this:
Locking a file in Python

How to get file to open with python app I made

For the past month I've been writing a desktop application for MacOS using Python. It requires opening files and saving compressed data to them. This application creates files using a my own made up extension, so the files are not usable for for other applications. I have almost everything figured out. However, I want to make it so that I can right click on a file with the extension and open it with my python application. I tried using sys.argv to get any arguments for the path of the file to open, but that doesn't work. I know there has to be a way. Preferably there's a builtin variable that is easy to use, but I haven't found anything that helps.
Any help would be useful.
Thanks.

python scripts creating csv file, with Archive check box is checked. How to create a csv file not archive?

I am facing a strange problem
Whenever my python scripts are creating any csv file, it is making them "Archive".
I mean in properties, Archive check box is checked.
Because of which it can't be read in later part of same script .
How can i create a csv file not archive?
Please help me resolve this problem.
Are you running a Windows OS? If yes, then this is not a problem with the Python CSV library. As about the error encountered while reading the CSV; you may want to re-check your python code for any flaws.
The Archive checkbox is actually an attribute of the file on Windows systems that indicates that the file needs to be backed up. Right click on any other file and you should see "Archive" checked.
Here are a couple of links that would give you more information
MSDN Technet discussion on File Attributes
Wikipedia article on Archive bit

Extract python script from exe generated with cx_Freeze

Is it possible to get .py text file from .exe file generated with cx_Freeze? If yes, how can I prevent it when I generate exe? I don't want that somebody see my python code. Of course anybody will have access to bytecode, but it much harder to disasemblate it.
Seems like using cython will make it impossible to get script back.

Python download file without external library

I have a very large Python script that I am using pyinstaller with to create an exe. I need to download an XML file but would like to keep the exe as small as possible as it is already getting quite large.
Is there a method within Python to get a file from a URL? I was not able to find anything without an external library
You can use urllib.urlretrieve() that saves the opened page to the specified path.
Alternatively you can open the url with urllib.urlopen() and then write the read file in the binary mode:
import urllib
urllib.urlretrieve(url, destination_path) # First and short way
with open(destination_path, 'wb') as f: # Equivalent to the first, but longer
f.write(urllib.urlopen(url).read())

Categories

Resources