Does Open Method in Python Executes an EXE - python

I wonder if open(file_name, "rb") as binary_file: pass does actually executes a file if it's exe? I am asking because I am reading some malicious files and viruses using Python stored as ".exe" files.

No it doesn't AND the flags 'rb' in your open statement stand for read binary. So it's only reading the file and putting it in a byte like object. So not only is it not executing (because that's not a function of open) it's only going to be opened in read mode.
You can read about the open function in the documentation.

Related

Nesting 'r' after opening a file in write mode

so I had an exercise asking to write to a file (using newlines) and then open it in reading mode. I did exactly that and the console outputs the right result. What happened was that I tried to do it within the 'w' mode code block and the console outputs nothing.
For example:
with open('test.txt', 'w') as wf:
nicknames = ["Big Tuna", "Prison Mike", "Booster Seat"]
wf.write('\n'.join(nicknames))
with open('test.txt', 'r') as rf:
print(rf.read())
I understand that the program only closes the file after the with statement, but I need clarification on my understanding of what's happening here.
So, what I understood so far is that the program first creates the test.txt file (test.txt did not exist in my file path) and then proceeds to convert and write the given contents into the test.txt file. After that, the nested with tries to open a file named test.txt in reading mode, but the console will not output anything because the program is trying to open a file that is already opened, that's why it cannot read into an already opened file.
Please correct me if I'm misunderstood the above because I am unsure whether or not I've understood it correctly, thank you.
That’s not what’s happening. Unix systems, at least, will happily let you open a file multiple times.
However, Python’s IO is buffered by default. You need to flush the data you’ve written out to the file before you can read the data from it. See https://docs.python.org/3/library/io.html#io.IOBase.flush for more information about this. (Summary: put wf.flush() after the wf.write(…) call and before attempting to read from it.

How to treat an 'x' file extension as a .txt file?

I want to rename a normal .txt file to .my_extension, not change its structure or anything.
Can I make python read that .my_extension as a .txt so it can read/write to it?
This line of code will treat the custom extension file as a txt file and write to it.
File = open('File_Name.custom_extention', 'w').write('Text')
Same goes with reading. Python actually does not look at the file extension.
But if you'd try to write to a file with a file structure of an exec it probably won't work :)
of course
actually python and almost every other language do not treat files by looking at the extension
they just do what you want
you can even have files with no extension and that's Ok
you can open a file just like this:
f = open('filename', 'mode')
to open a file
note that 'filename' should be the full name of file and the extension
somthing like 'myfile.myextension'
then write text into it like this
f.write('text to write in file')
see the list of modes in here
Yes you can work with opening and closing files and writing ,reading etc..
with this line of code :
output_file = open("your_file_name",mode="r")
the mode can specify the mode that you open the file for example if you want only open a file for reading type "r" ,for writing type "w",for appending type "a".
NOTE:
After opening the file in python you must close it by the close() statement (this may cause data leaking).
ex:
output_file.close()

Python - Write in text mode to file opened in binary mode

I am asking this out of curiosity.
What I am doing:
creating a temp file
writing data from a Pandas dataframe to it by using to_csv()
pushing the file to a FTP server
As the tempfile is opened in binary mode by default but the to_csv() method by default writes in text mode (which I need because I want to have UTF-8 as format) I am asking myself how you can write in text mode to a file opened in binary mode? I also need the binary format for the transfer to the FTP server.
What I did in detail:
I created a temp file like this:
fp = tempfile.NamedTemporaryFile(delete=False)
As I unterstand from the documentation the file is opened in binary mode.
tempfile.NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)
Then I saved my dataframe to the temp file like this:
df.to_csv(fp.name)
fp.flush()
fp.seek(0)
Also the to_csv() method states in the documentation that you need to open the file with newlines='' which only works in text mode. So I couldn't set the newline argument using a file opened in binary mode.
path_or_bufstr or file handle, default None
File path or object, if None is provided the result is returned as a string. If a file object is passed it should be opened with newline='', disabling universal newlines.
Then I used the storbinary() method from the ftplib to push the temp file to the FTP server. As I understand from the documentation the method requires a binary file.
FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)
Store a file in binary transfer mode. cmd should be an appropriate STOR command: "STOR filename". fp is a file object (opened in binary mode) which is read until EOF using its read() method in blocks of size blocksize to provide the data to be stored. The blocksize argument defaults to 8192. callback is an optional single parameter callable that is called on each block of data after it is sent. rest means the same thing as in the transfercmd() method.
For completeness I afterwards closed and deleted the file like this:
fp.close()
os.unlink(fp.name)
I thought about opening the tempfile in w+t mode so that it matches the to_csv() method, which recommends opening the file with newlines='' which only works in text mode. Also I need to specify the UTF-8 format for the CSV file which only works in text mode. ftplib's storbinary() method requires a file opened in binary mode. (storlines() method also does) so this doesn't fit.
So I opened the file in binary mode, wrote to it in text mode and transferred it using binary mode. Everything works and the result looks like I want it to but I am a bit confused if I am doing it the right way. How does writing in text mode to a file opened in binary mode work? I kind of assumed I would have to open the file in text mode in order to write in text mode to it using to_csv().
If anyone has a deeper knowledge about this and could clear up my confusion I would be very grateful. I don't like doing things not knowing why they work or if they should work haha.
Thanks!
This is quite broad question. Just briefly. This is all mostly about line endings. That's basically the only distinction between the binary and text modes.
If you "open" a file in the binary mode, all data are written exactly as they are. If you open a file in the text mode, newlines (\n) are converted according to the newline parameter.
I do not think that Pandas need the file to be opened in the text mode. If you open the file in the binary mode, then whatever Pandas writes will end up physically in the file. See line_terminatorstr parameter of the DataFrame.to_csv.
It's mostly the same with FTP. If you use storbinary, the file will be uploaded as is. If you use storlines, you let the FTP server convert the line endings.

How to ensure a file is closed for writing in Python?

The issue described here looked initially like it was solvable by just having the spreadsheet closed in Excel before running the program.
It transpires, however, that having Excel closed is a necessary, but not sufficient, condition. The issue still occurs, but not on every Windows machine, and not every time (sometimes it occurs after a single execution, sometimes two).
I've modified the program such that it now reads from one spreadsheet and writes to a different one, still the issue presents itself. I even go on to programmatically kill any lingering Python processes before running the program. Still no joy.
The openpyxl save() function instantiates ZipFile thus:
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
... with Zipfile then using that to attempt to open the file in mode 'wb' thus:
if isinstance(file, basestring):
self._filePassed = 0
self.filename = file
modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
try:
self.fp = open(file, modeDict[mode])
except IOError:
if mode == 'a':
mode = key = 'w'
self.fp = open(file, modeDict[mode])
else:
raise
According to the docs:
On Windows, 'b' appended to the mode opens the file in binary mode, so
there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows
makes a distinction between text and binary files; the end-of-line
characters in text files are automatically altered slightly when data
is read or written. This behind-the-scenes modification to file data
is fine for ASCII text files, but it’ll corrupt binary data like that
in JPEG or EXE files. Be very careful to use binary mode when reading
and writing such files. On Unix, it doesn’t hurt to append a 'b' to
the mode, so you can use it platform-independently for all binary
files.
... which explains why mode 'wb' must be used.
Is there something in Python file opening that could possibly leave the file in some state of "openness"?
Windows: 8
Python: 2.7.10
openpyxl: latest
Two suggestions:
First is to use with to close the file correctly.
with open("some.xls", "wb") as excel_file:
#Do something
At the end of that the file will close on its own (see this).
You can also make a copy of the file and work on the copied file.
import shutil
shutil.copyfile(src, dst)
https://docs.python.org/2/library/shutil.html#shutil.copyfile

Python Subprocess for Notepad

I am trying to open Notepad using popen and write something into it. I can't get my head around it. I can open Notepad using command:
notepadprocess=subprocess.Popen('notepad.exe')
I am trying to identify how can I write anything in the text file using python. Any help is appreciated.
You can at first write something into txt file (ex. foo.txt) and then open it with notepad:
import os
f = open('foo.txt','w')
f.write('Hello world!')
f.close()
os.system("notepad.exe foo.txt")
You may be confusing the concept of (text) file with the processes that manipulate them.
Notepad is a program, of which you can create a process. A file, on the other hand, is just a structure on your hard drive.
From a programming standpoint, Notepad doesn't edit files. It:
reads a file into computer memory
modifies the content of that memory
writes that memory back into a file (which could be similarly named, or otherwise - which is known as the "Save as" operation).
Your program, just as any other program, can manipulate files, just as notepad does. In particular, you can perform exactly the same sequence as Notepad:
my_file= "myfile.txt" #the name/path of the file
with open(file, "rb") as f: #open the file for reading
content= f.read() #read the file into memory
content+= "mytext" #change the memory
with open(file, "wb") as f: #open the file for writing
f.write( content ) #write the memory into the file
Found the exact solution from Alex K's comment. I used pywinauto to perform this task.

Categories

Resources