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 7489173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:12:36+00:00 2026-05-29T15:12:36+00:00

I’ve been searching online for a while now and can’t seem to find anything.

  • 0

I’ve been searching online for a while now and can’t seem to find anything. I’m basically learning a few languages and I am just trying to recreate a program in different languages.

def read_one_file():
    f = open('C:\Python27\inventory.dat', 'r')
    invid = f.readline()
    stock = f.readline()
    published = f.readline()
    price = f.readline()
    invtype = f.readline()
    title = f.readline()
    author = f.readline()
    return invid, stock, published, price, invtype, title, author

read_one_file()

print "Update Number In Stock"
print "----------------------"
print "Item ID: ", invid

Basically I’m trying to read in a file, absorb the data into variables then pass those variables to the main(class?). When I return them they’re still not able to be printed. When I initialize them outside of read_one_file they still don’t return the right thing.

  • 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-29T15:12:37+00:00Added an answer on May 29, 2026 at 3:12 pm

    You need to store the results of read_one_file() somewhere. What you’re really
    doing with your return statement is creating a tuple of the results. You then
    have to unpack that tuple when you call read_one_file. Here is an example:

    (invid, stock, published, price, invtype, title, author) = read_one_file()
    
    print "Item ID:", invid
    

    This syntax is performing something called “pattern matching” and what it does
    is break up the tuple that read_one_file returns and gives names to each of
    element in it. I added the parenthesis here to make it clearer that
    read_one_file is returning a tuple, but you could just as easily write it like
    this:

    invid, stock, published, price, invtype, title, author = read_one_file()
    

    That’s all well and good, but really, this is sort of a bad way to return things
    from your function. If you have a lot of variables you want to return from a
    function like this, a dict is probably a better way to go. Also, you’ll
    probably want to use a with statement to ensure your file is closed and cleaned
    up properly once you’re done with it. Here is what your code would look like
    using that strategy:

    def read_one_file():
        with open('C:\Python27\inventory.dat', 'r') as f:
            return dict(invid = f.readline().strip(),
                        stock = f.readline().strip(),
                        published = f.readline().strip(),
                        price = f.readline().strip(),
                        invtype = f.readline().strip(),
                        title = f.readline().strip(),
                        author = f.readline().strip())
    
    results = read_one_file()
    
    print "Update Number In Stock"
    print "----------------------"
    print "Item ID: ", results['invid']
    

    Edit: I changed the code above to use .strip(), since (as @NiklasB. pointed out), newlines are still included with just readline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.