Python download file without external library - python

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())

Related

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.

How to open xlsx file in LibreOffice on Ubuntu?

I want to open a file in LibreOffice Calc on Ubuntu after working on it through Python.
How would I go about this?
I tried:
import subprocess
subprocess.call("explorer path-to-file")
# got error that the path doesn't exist
subprocess.call("calc path-to-file")
# Calc is not executable type error
You could use the xdg-open tool (if you have it) to use the default tool for the file type, or if you really want to always use Libreoffice, the executable for it is libreoffice.
os.system("xdg-open path-to-file")
os.system("libreoffice path-to-file")
(and as always when using os.system(), make sure path-to-file comes from a trusted source.)

Getting python to run an application when the application needs an input file

import subprocess
subprocess.call(['C:\\Users\michael\\Desktop\\Test\\pdftotext'])
pdftotext is the application that will run if I use this ^ code. This works fine, however, I'm trying to find a way to run pdftotext that includes the pdf's file name which pdftotext uses to convert it into a text file.
Note this is NOT a question about pdftotext.
When I use cmd in windows to run this I simply type pdftotext fileName.pdf and it converts the pdf file into a text file, no problem. Now I want to do something equivalent with Python.
I changed it to this, but it doesn't work. I'm told "The system cannot find the file specified" and I've put pdftotext in the src file along with filename.pdf
import subprocess
subprocess.call(['C:\\Users\michael\\Desktop\\Test\\pdftotext', 'filename.pdf'])
subprocess.call takes an iterable where the first item is the executable and the following are switches and parameters.
This means you need to change the above to this:
import subprocess
subprocess.call(['C:\\Users\michael\\Desktop\\Test\\pdftotext', 'filename.pdf'])

In which directory should I save my .dat file?

I am currently writing a very simple script on python which requires that I download a .dat file posted online. I then use loadtxt to plot the data using matplotlib. However, in order to read the file, I had to change directory (os.chdir) to 'downloads' (where the file had been saved). This works fine for me, but I will need to send the script to somebody else, in which case it seems as though the directory would again need to be something else in order to find the file... Where might I save the file so that no matter to whom I send it, the script will run properly?
User-specific path to the dat file can be acquired using:
os.chdir(raw_input('Which is the path to the download folder?'))

Openin exe binary files and editing

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

Categories

Resources