PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How to show the output in multiple text files in python, for example: print digits from 1 to 10 in 1.txt,2.txt,3.txt

 August 17, 2022     file-io, output, python, python-3.x, text-files     No comments   

Issue

How to show the output in multiple text files in python, for example: print digits from 1 to 10 in 1.txt(o/p-1),2.txt(o/p-2),3.txt(o/p-3) files... Here is a piece of code that I tried, to get the image files as text and store it in individual folders.

     c=-1

     outFile=[] //created a list,as it would overwrite the same outfile multiple times if not used(expecting it to be a different file when list is used)


     for i in range(0, len(onlyfiles)):
                

   `     
         text = pytesseract.image_to_string(images[i])

         c=c+1

         outFile.append(c)

         outFile[c] = open(str1+"outputfile.txt", "w")//str values could be incremented using a different loop/function to have difference in name.
  
         outFile[c].write(text)

         outFile[c].close()

Any modification or new approach is really appreciated.


Solution

for i in images:
    text = pytesseract.image_to_string(i)
    with open(f"{i}.txt", w) as f:
        f.write(text)

Assumption:

  1. We will save the text files (1.txt, 2.txt, .... ) in current directory.
  2. images is an array containing multiple images (not the physical location)


Answered By - Aman Neo
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing