Oli
Oli

Reputation: 21

How to have an integer in a filename (python)

I am making a code that will create textfiles equal to a number they entered, all named after them. So far i have this

name = input("Enter your name")  
num = input("Enter a number")  
x = 0
for i in range(1,num):
   x = x+1
   file = open(name(x) , "w+")
   lines = ("hi" , name)
   file.writelines(lines)
file.close()

but the name 'name(x)" won't work as a variable name, are there any ways of having variable names like x1, x2, x3 ect with an inputted number?

Upvotes: 1

Views: 13659

Answers (4)

DirtyBit
DirtyBit

Reputation: 16772

Change the integer x to string:

name = input("Enter your name")  
num = input("Enter a number")  
for i in range(1,num):
   file = open(name + str(i) , "w+")
   lines = ("hi" , name)
   file.writelines(lines)
   file.close()

Upvotes: 0

Gsk
Gsk

Reputation: 2945

use formatting:

instead of name(x) you have to format a string:

"{0}{1}.txt".format(name, x)

the variable name will be placed on the {0} placeholder, the variable x will be placed on the {1} placeholder.

this means that if name == "Answer" and x = 42, the file name will be Answer42.txt

the formatting can be in any way you want:

"File_{1}_{0}_number{1}.txt".format(name, x)

will become: File_42_Answer_number42.txt;

or for example your line variable could be:

line = "Welcome {0}, How are you? you have opened {1} file until now!".format(name, x)

Upvotes: 5

Jérôme
Jérôme

Reputation: 14674

I guess you want to do

file = open(name + str(x) , "w+")

or

file = open('{}{}'.format(name, x) , "w+")

In the first line, the + operator concatenates name (which is a string) with str(x). The conversion to string is necessary.

In the second line, format does the conversion automatically.

You could rewrite the whole loop without x:

name = input("Enter your name")
num = input("Enter a number")

for i in range(1, int(num)):
   with open('{}{}'.format(name, i) , "w+") as file:
       lines = ('hi', name)
       file.writelines(lines)

Upvotes: 1

mas
mas

Reputation: 1245

name = input("Enter your name")  
num = input("Enter a number")  
x = 0
for i in range(1,int(num)):
   x = x+1
   file = open(str(x), "w+")
   lines = ("hi" , name)
   file.writelines(lines)
file.close()

Should do the trick. the str() wrapper around x in open() may even be necessary (I can never remember if something autoconverts to str or not). Furthermore, you had something strange going on in that line with name(x). You haven't defined name() to be a function so it should have been giving you a big error.

input() returns a string, as user5173426 said in the comments. You have to explicitly cast it as an int with int() in order to perform numerical operations on it (range() expects a number, it does not cast strings to int).

Furthermore, you are defining lines to be a tuple of two strings. You may consider changing it to lines = "hi" + name or even lines = f"hi {name}". I don't think you're going to get the output you're expecting in this case.

Upvotes: 0

Related Questions