python: simulate writing to a file object without creating a file

  • Last Update :
  • Techknowledgy :

Since the PdfFileMerger.write method supports writing to file-like objects, you can simply make the PdfFileMerger object write to a BytesIO object instead:

from io
import BytesIO

merger = PdfFileMerger()

for pdf in files_to_merge:
   merger.append(pdf)

output = BytesIO()
merger.write(output)
merger.close()

file_content = output.getvalue()

Suggestion : 2

Posted on Feb 20, 2020 , Joined Jan 28, 2020

>>>
import io
   >>>
   >>>
   myFile = io.StringIO()
>>> myFile = io.StringIO("Data into the file") >>>
   myFile.read()
'Data into the file'
>>> myFile.read()
''
>>> myFile.seek(0)
0
   >>>
   myFile.read()
'Data into the file' >>>
>>>
import io
   >>>
   myFile = io.StringIO("Feeling good") >>>
   data = myFile.read() >>>
   print(data)
Feeling good
   >>>
>>> myFile = io.StringIO("") >>>
   myFile.write("Write a line into the file\n") >>>
   myFile.write("Second line.\n")

Suggestion : 3

September 3, 2018May 18, 2021

1._
#create file
file_obj = open("filename", "mode")

We are using w mode to create a file and writing a text in the file.

f = open("cFile.txt", "w")
f.write(" Created file")

In this example, we are creating pdf and image files.

pf = open("picFile.png", "w")
jf = open("imgFile.jpg", "w")
pdff = open("pdfFile.pdf", "w")

It will returnTrue for both files or directories but you can instead use if the file is specified.

os.path.isfile(file_path)

Or use this code first check file exists or not, then do create it. 

import os.path
file_exists = os.path.isfile(filename)

if file_exists:
   # do something
else:
   # do something
   else

Suggestion : 4

When you write a method that accepts a file as an argument, you can silently accommodate callers who pass in strings by wrapping in a StringIO any string that gets passed in:,The Adapter is a common design pattern: to make an object acceptable as input to a method, it's wrapped in another object that presents the appropriate interface. The StringIO class is an Adapter between String and File (or IO), designed for use with methods that work on File or IO instances. With a StringIO, you can disguise a string as a file and use those methods without them ever knowing they haven't really been given a file.,The StringIO class wraps a string in the interface of the IO class. You can treat it like a file, then get everything that's been "written" to it by calling its string method.,Just remember that, unlike a string, you can't iterate over a file multiple times without calling rewind:

Here's a StringIO used as an input source:

	require 'stringio'
	s = StringIO.new % {
	      I am the very model of a modern major general.
	      I 've information vegetable, animal, and mineral.}

	      s.pos # => 0
	      s.each_line {
	         | x | puts x
	      }
	      # I am the very model of a modern major general.
	      # I 've information vegetable, animal, and mineral.
	      s.eof ? # => true
	      s.pos # => 95
	      s.rewind
	      s.pos # => 0
	      s.grep / general /
	      # => ["I am the very model of a modern major general.
	         "]

Here are StringIO objects used as output sinks:

	s = StringIO.new
	s.write('Treat it like a file.')
	s.rewind
	s.write("Act like it's")
	s.string # => "Act like it's a file."

	require 'yaml'
	s = StringIO.new
	YAML.dump(['A list of', 3,: items], s)
	puts s.string
	#-- -
	# - A list of
	   # - 3
	# -: items

StringIO-type functionality is less necessary in Ruby than in languages like Python, because in Ruby, strings and files implement a lot of the same methods to begin with. Often you can get away with simply using these common methods. For instance, if all you're doing is writing to an output sink, you don't need a StringIO object, because String#<< and File#<< work the same way:

	def make_more_interesting(io)
	io << "… OF DOOM!"
	end

	make_more_interesting("Cherry pie") # => "Cherry pie… OF DOOM!"

	open('interesting_things', 'w') do | f |
	      f.write("Nightstand")
	   make_more_interesting(f)
	end
	open('interesting_things') {
	   | f | f.read
	}
	# => "Nightstand… OF DOOM!"

and a file containing that string:

	output = open("poem", "w")
	output.write(poem)
	output.close
	input = open("poem")

will give the same result when you call an Enumerable method:

	poem.grep / ed$ /
	   # => ["Whence all but he had fled
	      ", "
	      Before he went to bed "]
	      input.grep / ed$ /
	      # => ["Whence all but he had fled
	         ", "
	         Before he went to bed "]