Python os.popen requires an integer? - python

I'm running python 2.7.3 and doing some basic stuff involving the os module.
import os
def main():
f= os.popen('cat > out', 'w',1)
os.write(f, 'hello pipe')
os.close(f)
main()
Based on examples I've seen I would expect the code to work, but the interpreter gives this error:
Traceback (most recent call last):
File "./test.py", line 11, in <module>
main()
File "./test.py", line 8, in main
os.write(f, 'hello pipe')
TypeError: an integer is required
Okay, off to the documentation. The help page says:
write(...)
write(fd, string) -> byteswritten
Write a string to a file descriptor.
fd seems to stand for file descriptor. Presumably this is what you get when you do something like:
file = open('test.py')
No surprise, online documentation says the exact same thing.
What's going on here?

No, a "file descriptor" is an integer, not the file object. To go from a file object to a file descroptor, call file.fileno(). To wit:
>>> f = open("tmp.txt", "w")
>>> help(f.fileno)
Help on built-in function fileno:
fileno(...)
fileno() -> integer "file descriptor".
This is needed for lower-level file interfaces, such os.read().
>>> f.fileno()
4
Instead of using that, though, you probably just want to do the following, unless you really need to use the low-level functions for some reason:
f = os.popen('cat > out', 'w',1)
f.write('hello pipe')
f.close()

Related

Print traceback to file when there is no exception

What specific Python 3 syntax must be changed below in order to successfully print a trackback form a successfully running function into a file that is located at aValidFileNameAndPath?
We need this to work in normal running functions where there is NO exception.
The Python 3 code we are using is:
import traceback
traceback.print_stack(file=aValidFileNameAndPath)
The error thrown by the above code is:
File "C:\path\to\script\in\our\app\ourScriptName.py", line 69, in ourFunctionName
traceback.print_stack(file=aValidFileNameAndPath)
File "C:\Program Files\Python39\lib\traceback.py", line 190, in print_stack
print_list(extract_stack(f, limit=limit), file=file)
File "C:\Program Files\Python39\lib\traceback.py", line 25, in print_list
print(item, file=file, end="")
AttributeError: 'str' object has no attribute 'write'
The other postings we have found on Stack Overflow have to do with printing exceptions. We do NOT want to print an exception. Instead, we just want to print out the chain of functions that called a specific function during normal functioning of the app when there is no exception to be thrown.
You are getting this error because where you are using the file path the code wants a file object.
To make a file object, use open, e.g. myfile = open(aValidFileNameAndPath)
You will also want to set the file to writing mode, e.g. open(path, 'w')
Then you can pass myfile as a paremeter, e.g. traceback.print_stack(file=myfile)
Then make sure to close the file with close
Here is a full example:
import traceback
myfile = open(aValidFileNameAndPath, 'w') # note that this will delete anything that alredy exists in the file
traceback.print_stack(file=myfile)
myfile.close()

What is wrong with my python movie database code?

I was looking for some help with a program I am writing for personal use. It is supposed to export a movie's name and rating (stored in a dictionary) to an external file (file.txt) and then load it at the start of every run. It should also export a movie's name and review (also a dictionary) to another external file (review.txt). I get an error on the line for reading the file.txt and putting it in base. Any clues?
base = {}
#Open and write info to base
with open('file.txt','r') as f:
# Error here :(
base = eval(f.read())
error message:
Traceback (most recent call last):
File "/Users/Will/Documents/movie_database.py", line 18, in <module>
base = eval(f.read())
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
Your file is empty, so eval() throws an exception because no Python expression was found:
>>> eval('') # empty string
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
You shouldn't be using eval() in the first place however, the security implications are terrifying here. Someone that can fool you into loading an arbitrary file can now take over your Python process, and turn your machine into a spam bot zombie.
json or shelve (or directly using pickle, although that has security implications as well) would offer you better and more robust serialisation options. You could also look into using sqlite3 for a SQL database option.

How do I save a list to a pickle file in a temporary directory and pass that file into a function?

The issue is that I'm pulling data from one source and I want to save it to dropbox as a pickle file. I can't save it in a directory, because I'm running the code on a server (iron.io).
import tempfile
import pickle
def SFDCDropboxSync(Data):
f = tempfile.NamedTemporaryFile(delete=False)
pickle.dump(Data,open(f,'wb'))
client = dropbox.client.DropboxClient(access_token)
client.put_file(filename, f)
This is the error I get:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Shippy/RecurringDataDump/SFDCDropboxUpload.py", line 38, in <module>
if __name__ == "__main__": main() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Shippy/RecurringDataDump/SFDCDropboxUpload.py", line 31, in main
print SFDCDropboxUploadDownload().SFDCDropboxSync(lst) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Shippy/RecurringDataDump/SFDCDropboxUpload.py", line 26, in SFDCDropboxSync
pkl = self.SaveListtoPickle(lst) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Shippy/RecurringDataDump/SFDCDropboxUpload.py", line 20, in SaveListtoPickle
pickle.dump(lst,open(f,'wb')) TypeError: coercing to Unicode: need string or buffer, instance found [Finished in 0.7s with exit code 1] [shell_cmd: python -u "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Shippy/RecurringDataDump/SFDCDropboxUpload.py"] [dir: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Shippy/RecurringDataDump] [path: /usr/bin:/bin:/usr/sbin:/sbin]
In your code, the NamedTemporaryFile f is not a string. It is a file object, similar to the output of open(file_path).
From the documentation: This file-like object can be used in a with statement, just like a normal file.
If you want to path to the created file, use tmp_file.name
For example, this works: (tested on python 3.6.2)
def SFDCDropboxSync(Data):
with tempfile.NamedTemporaryFile() as tmp_file:
pickle.dump(Data, tmp_file)
tmp_file.flush()
print(pickle.load(open(tmp_file.name, 'rb')))
This will delete the file when it exits the while (file closes).
Warning for Windows: you might have trouble reading the file while it is open. Instead, use something similar to this:
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
pickle.dump(Data, open(tmp_file.name, 'wb'))
tmp_filename = tmp_file.name
pickle.load(open(tmp_filename, 'rb'))
os.remove(tmp_filename)

Python 2.6: Opening 4GB file throws "IOError: [Errno 127] Value too large to be stored in data type" on AIX

When opening 4GB+ file on AIX Python 2.6.2 I receive IOError:
>>> fd = open('/mnt/t/MY_BIG_4GB_FILE', 'r')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 127] Value too large to be stored in data type: '/mnt/t/MY_BIG_4GB_FILE'
Any solutions? I didn't find information in Google.
EDIT:
To read BIG file I do something line that (I know that it is rubbish but sometimes You simply can't change python version):
from subprocess import Popen, PIPE
p = Popen(["cat", source_file], stdout=PIPE, bufsize=BUFFER_SIZE)
try:
for line in iter(p.stdout.readline, ''):
# process line
pass
finally:
p.communicate() # closing Popen
You're looking for "large file support." There is a decent brief here: http://docs.python.org/2/library/posix.html#large-file-support . You likely need to recompile your Python interpreter with the appropriate options, or find a pre-built one that has those options. Try a build of Python 2.7 if you can, while you're at it.

where is the call to encode the string or force the string to need to be encoded in this file?

I know this may seem rude or mean or unpolite, but I need some help to try to figure out why I cant call window.loadPvmFile("f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm") exactly like that as a string. Instead of doing that, it gives me a traceback error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 415, in <module>
window.loadPvmFile("f:\games\#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")
File "F:\Python Apps\pvmViewer_v1_1.py", line 392, in loadPvmFile
file1 = open(path, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename:
'f:\\games\\#DD.ATC3.Root\\common\\models\x07300\x07mu\\dummy.pvm'
Also notice, that in the traceback error, the file path is different. When I try a path that has no letters in it except for the drive letter and filename, it throws this error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 416, in <module>
loadPvmFile('f:\0\0\dummy.pvm')
File "F:\Python Apps\pvmViewer_v1_1.py", line 393, in loadPvmFile
file1 = open(path, "r")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
I have searched for the place that the encode function is called or where the argument is encoded and cant find it. Flat out, I am out of ideas, frustrated and I have nowhere else to go. The source code can be found here: PVM VIEWER
Also note that you will not be able to run this code and load a pvm file and that I am using portable python 2.7.3! Thanks for everyone's time and effort!
\a and \0 are escape sequences. Use r'' (or R'') around the string to mark it as a raw string.
window.loadPvmFile(r"f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")

Categories

Resources