0

I have the following loop, where cfile has been defined as 'C_' previously.

for i in range(15,25):
    infile = cfile + str(i+1) + '.txt'
    C + str(i+1) = np.loadtxt(infile, delimiter=',')

I have many files in the working directory named C_1, C_2, etc.

I'd like to import these files into Python and name them as C_1, C_2, etc, but I'd only like to do so for that certain range. I am not understanding why the str(i+1) will not work.

4
  • Which version of Python are you using? Commented Jan 19, 2021 at 1:10
  • 1
    Does this answer your question? How do I create variable variables? and SyntaxError: cannot assign to operator Commented Jan 19, 2021 at 1:12
  • BTW, "not working" is generally not helpful. For debugging, we need to know exactly what error you're getting, preferably with the full traceback. See minimal reproducible example for reference, and How to Ask if you want other tips. Commented Jan 19, 2021 at 1:14
  • 2
    I would recommend you use dictionaries or append these files to a list named C then each variable will be C[0] instead of C_0 and you'll be able to do some logic based on the numbers too Commented Jan 19, 2021 at 1:16

2 Answers 2

1

You can use glob to open all files with a specified pattern. Although it's patterns follow Unix guidelines and may need to be further filtered to meet your specifications:

import glob

files = glob.glob("C_[1-2][1-9].txt")

Or just load all the correct files into a dict as it's the recommended way of creating a variable amount of variables.

files = {f"C{i}": np.loadtxt(f"{cfile}{i}.txt", delimiter=',') for i in range(16,26)}
Sign up to request clarification or add additional context in comments.

Comments

0
cfile='C_'
C='What_'
H={}
for i in range(15,25):
    infile = cfile + str(i+1) + '.txt'
    #print("infile:",infile)
    k=C+str(i+1)
    H[k] = np.loadtxt(infile, delimiter=',')
    print(f"Your loaded file is here: H[{k}]")

    #PRINT OUT THE CONTENT OF THE FILE 
    print(H[k])
    
 

just use print(H[k]) to print out the content of each file. Or print( H["What_24"] ), or if you change the string of variable C = 'C_' then you can print(H["C_24"] ) and view the content of file stored inside the dict.

Why what you wanted to do was an error:

C + str(i+1) = np.loadtxt(infile, delimiter=',')

Because C is undefined ... and if it were defined like C='What_you_want' you would have gotten:

'What_you_want'+str(i+1)  ->   'What_you_want'+'number' -> 'What_you_wantnumber' 

So

'What_you_wantnumber' is a string which resides inside the variable C

np.loadtxt(infile, delimiter=',') is a method that return the content o the file as a string .

so you can't assign string = string . But you can do

variable = string

for example: the variable name = "flagello" ok

 print(name)  

output : flagello #that is as string

"flagello"="flagello"
  File "<stdin>", line 1
SyntaxError: can't assign to literal
"flagello"="silicio"
  File "<stdin>", line 1
SyntaxError: can't assign to literal
'flagello' == 'silico'  

Output: False

'flagello' == 'flagello'

Output: True

You can compare strings  but you can t assing a string to a string .

You can assign a string value  to a variable. 

But at this moment nothing rename nothing . Now you get the content of the files , assign the content of each file inside a dict ad print the content of the dict

How to rename a file

import os
os.rename(r'C_21.txt',r'CCCP_21.txt')

In this case you don't need to read the content of the file to rename it.

Just use the old name and the new name

3 Comments

This worked, but I am not understanding why I cannot use print(C_24) to pull up the file. I continue to get the NameError: name is not defined, which I thought the loop should have taken care of.
See edited answer: just use print(H[k]) to print out the content of each file. Or print(H["What_24"] ), or if you change the string of variable C = 'C_' then you can print(H["C_24"] )
I hope I have clarified your doubts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.