io

Why doesn't tempfile.SpooledTemporaryFile implement readable, writable, seekable?

Why doesn't tempfile.SpooledTemporaryFile implement readable, writable, seekable? Question: In Python 3.6.1, I’ve tried wrapping a tempfile.SpooledTemporaryFile in an io.TextIOWrapper: with tempfile.SpooledTemporaryFile() as tfh: do_some_download(tfh) tfh.seek(0) wrapper = io.TextIOWrapper(tfh, encoding=’utf-8′) yield from do_some_text_formatting(wrapper) The line wrapper = io.TextIOWrapper(tfh, encoding=’utf-8′) gives me an error: AttributeError: ‘SpooledTemporaryFile’ object has no attribute ‘readable’ If I create a simple class …

Total answers: 2

Python can't open file "No such file or directory"

Python can't open file "No such file or directory" Question: def main(): fh = open(‘lines.txt’) for line in fh.readlines(): print(line) if __name__ == “__main__”: main() Directory files I am on for-working.py file, and am trying to access the lines.txt file within the same working directory. But I get error No such file or directory: ‘lines.txt’ …

Total answers: 4

How do you pass a generated PDF to an API as an IO stream without writing it to disk?

How do you pass a generated PDF to an API as an IO stream without writing it to disk? Question: I am using PyPDF2 to generate a PDF, and I would like to upload this PDF to Cloudinary, which accepts images as IO objects. The example from their docs: cloudinary.uploader.upload(open(‘/tmp/image1.jpg’, ‘rb’)) In my application, I …

Total answers: 1

Python_io in tensorflow

Python_io in tensorflow Question: I’m having trouble working with tensorflow. I want to use TFRecordWriter() as below: with tf.python_io.TFRecordWriter(testing_filename) as tfrecord_writer: # do sth but I get the error: AttributeError: module ‘tensorflow’ has no attribute ‘python_io’ I’m working with tensorflow 1.2 and python 3. How can I fix the problem? Thanks. Asked By: mohamad danesh …

Total answers: 4

How to create in-memory file object

How to create in-memory file object Question: I want to make a in-memory file to use in pygame mixer. I mean something like http://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load which says load() method supports file object. import requests from pygame import mixer r = requests.get("http://example.com/some_small_file.mp3") in_memory_file = file(r.content) # something like this mixer.music.init() mixer.music.load(in_memory_file) mixer.music.play() Asked By: FKLC || Source …

Total answers: 1

How to read/print the ( _io.TextIOWrapper) data?

How to read/print the ( _io.TextIOWrapper) data? Question: With the following code I want to > open a file > read the contents and strip the non-required lines > then write the data to the file and also read the file for downstream analyses. with open(“chr2_head25.gtf”, ‘r’) as f, open(‘test_output.txt’, ‘w+’) as f2: for lines …

Total answers: 1

Difference between `open` and `io.BytesIO` in binary streams

Difference between `open` and `io.BytesIO` in binary streams Question: I’m learning about working with streams in Python and I noticed that the IO docs say the following: The easiest way to create a binary stream is with open() with ‘b’ in the mode string: f = open("myfile.jpg", "rb") In-memory binary streams are also available as …

Total answers: 2

Reading file using relative path in python project

Reading a file using a relative path in a Python project Question: Say I have a Python project that is structured as follows: project /data test.csv /package __init__.py module.py main.py __init__.py: from .module import test module.py: import csv with open("..data/test.csv") as f: test = [line for line in csv.reader(f)] main.py: import package print(package.test) When I …

Total answers: 6

When should I ever use file.read() or file.readlines()?

When should I ever use file.read() or file.readlines()? Question: I noticed that if I iterate over a file that I opened, it is much faster to iterate over it without “read”-ing it. i.e. l = open(‘file’,’r’) for line in l: pass (or code) is much faster than l = open(‘file’,’r’) for line in l.read() / …

Total answers: 5

How to write to a CSV line by line?

How to write to a CSV line by line? Question: I have data which is being accessed via http request and is sent back by the server in a comma separated format, I have the following code : site= ‘www.example.com’ hdr = {‘User-Agent’: ‘Mozilla/5.0’} req = urllib2.Request(site,headers=hdr) page = urllib2.urlopen(req) soup = BeautifulSoup(page) soup = …

Total answers: 6