# 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?
If you want to store per-file data in a data structure, you’ll need to use a
dictor similar to do so: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
datawill have three keys, each pointing to the 3 characters read from each file: