Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6047971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:24:39+00:00 2026-05-23T07:24:39+00:00

I am writing a python script in order to write a tex file. But

  • 0

I am writing a python script in order to write a tex file. But I had to use some information from another file. Such file has names of menus in each line that I need to use. I use split to have a list for each line of my “menu”.
For example, I had to write a section with the each second element of my lists but after running, I got anything, what could I do?

This is roughly what I am doing:

    texfile = open(outputtex.tex', 'w')
    infile = open(txtfile.txt, 'r')
        for line in infile.readlines():
            linesplit = line.split('^')
            for i in range(1,len(infile.readlines())):
               texfile.write('\section{}\n'.format(linesplit[1])) 
               texfile.write('\\begin{figure*}[h!]\n')
               texfile.write('\centering\n')
               texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' %i)
               texfile.write('\end{figure*}\n')
               texfile.write('\\newpage\n')
    texfile.write('\end{document}')
    texfile.close()

By the way, in the inclugraphics line, I had to increace the number after pg_ from “0001” to “25050”. Any clues??

I really appreciate your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:24:40+00:00Added an answer on May 23, 2026 at 7:24 am

    I don’t quite follow your question. But I see several errors in your code. Most importantly:

        for line in infile.readlines():
        ...
        ...
            for i in range(1,len(infile.readlines())):
    

    Once you read a file, it’s gone. (You can get it back, but in this case there’s no point.) That means that the second call to readlines is yielding nothing, so len(infile.readlines()) == 0. Assuming what you’ve written here really is what you want to do (i.e. write file_len * (file_len - 1) + 1 lines?) then perhaps you should save the file to a list. Also, you didn’t put quotes around your filenames, and your indentation is strange. Try this:

    with open('txtfile.txt', 'r') as infile:    # (with automatically closes infile)
        in_lines = infile.readlines()
    in_len = len(in_lines)
    
    texfile = open('outputtex.tex', 'w')
    for line in in_lines:
        linesplit = line.split('^')
        for i in range(1, in_len):
            texfile.write('\section{}\n'.format(linesplit[1])) 
            texfile.write('\\begin{figure*}[h!]\n')
            texfile.write('\centering\n')
            texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' %i)
            texfile.write('\end{figure*}\n')
            texfile.write('\\newpage\n')
    texfile.write('\end{document}')
    texfile.close()
    

    Perhaps you don’t actually want nested loops?

    infile = open('txtfile.txt', 'r')
    texfile = open('outputtex.tex', 'w')
    for line_number, line in enumerate(infile):
        linesplit = line.split('^')
        texfile.write('\section{{{0}}}\n'.format(linesplit[1])) 
        texfile.write('\\begin{figure*}[h!]\n')
        texfile.write('\centering\n')
        texfile.write('\includegraphics[scale=0.95]{pg_000%i.pdf}\n' % line_number)
        texfile.write('\end{figure*}\n')
        texfile.write('\\newpage\n')
    texfile.write('\end{document}')
    texfile.close()
    infile.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script in python which basically queries WMI and updates the information
I ended up writing a quick little script for this in Python, but I
I am writing a python script (Linux) that is adding some shell aliases (writes
I'm writing a script to automate some command line commands in Python. At the
I'm writing a python script that executes a csh script in Solaris 10. The
I am new to python and am writing some scripts to automate downloading files
I'm writing a Python program with a lot of file access. It's running surprisingly
So I've decided to try to solve my physics homework by writing some python
Folks, I have a problem. I am a writing a script in python which
I am writing python scripts and execute them in a Makefile. The python script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.