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

  • SEARCH
  • Home
  • 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 9064499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:16:08+00:00 2026-06-16T16:16:08+00:00

# Define the project name and path for input files # need to use

  • 0
# Define the project name and path for input files
# need to use \ before \t and \ to print these characters
Project = "101"
path_Directory =  "C:\Users\\tp\Desktop\project\\"
full_path_Directory = path_Directory + Project

# Set path for files in the program
File_stock = full_path_Directory + "_stock.txt"
File_exchange = full_path_Directory + "_exchange.txt"
File_country = full_path_Directory + "_country.txt"

var_set = [File_stock, File_exchange, File_country]

  for var in var_set:
    var = open(var,'r')
    var = var.read()[3:]
    print var

I created a var_set to contain the 3 variables – File_stock, File_exchange and File_country because I thought it would be more efficient to loop through the repetitive tasks. While the output from the code shown above is correct, but when i typed: “print stock” or “print exchange” or print “country”, the output is always wrong because it shows the path directory instead of the data from the text files.

Does anyone know what could be the reason and how should I fix it?

  • 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-06-16T16:16:09+00:00Added an answer on June 16, 2026 at 4:16 pm

    If you want to store per-file data in a data structure, you’ll need to use a dict or similar to do so:

    Project = "101"
    
    path_Directory =  r"C:\Users\tp\Desktop\project"
    names = ['stock', 'exchange', 'country']
    data = {}
    
    for name in names:
        path = os.path.join(path_Directory, '{0}_{1}.txt'.format(Project, name))
        data[name] = open(path, 'r').read(3)
    

    I also replaced your .read()[:3] with .read(3) to just read three characters instead of reading the whole file; it’s more efficient.

    Now data will have three keys, each pointing to the 3 characters read from each file:

    print data['stock']  # prints 'xyz'. etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using define in my project. My js code woks when I don't
I am working on a project that requires me to define a DSL. Since
How can I define one global GridViewColumnHeader.Click handler for any ListView.GridViewColumnHeader in my project?
As I know I can define a new Enum DataFormat at the my project
I have set up a BlackBerry Cascades-UI project. I am using QML to define
Using the following pseudo-code: #define BUILD_PATH C:/MyBuild/ #define BUILD_NAME mydll.dll // Set build path
I am trying to organize my project by splitting commands up into separate files
I am using following code in the cakephp project in bootstrap to define constants
Apparently I'm missing something while trying to use a schema.ini to define a csv.
I need to generate default Blueprint CSS files using a Ruby command in the

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.