NumberOneRobot
NumberOneRobot

Reputation: 1777

How do I name a file by a variable name in Python?

I want to take as input the name of a network, and then I want a file to be saved using the same name as the variable. Is there a way to take a variable and then name a file the variable name?

For instance, say I have the network called facebook saved as a dictionary. Can I somehow take that variable name and use it as a filename?

This is all in Python.

Thank you!

Upvotes: 1

Views: 59108

Answers (6)

CarpelTunnel
CarpelTunnel

Reputation: 11

I'm trying this approach and I'm getting a very odd issue, my items are being saved to the file a program run behind. So if I run the file once, nothing will happen. A second time, the information from the run will be saved.

f = open("results_{0}.txt".format(counter), 'w')
f.write("Agent A vs. Champion\n"
      + "Champion wins = "
      + str(winsA1)
      + " Agent A wins = "
      + str(winsB1))
f.write("\n\nAgent B vs. Champion\n"
      + "Champion wins = "
      + str(winsA2)
      + " Agent B wins = "
      + str(winsB2))
f.write("\n\nRandom vs. Champion\n"
      + "Champion wins = "
      + str(winsA3)
      + " Random wins = "
      + str(winsB3))
f.close()

Upvotes: 0

8-Track
8-Track

Reputation: 99

This is a simple way to have the file name be something that the user inputs:

#Here the user inputs what the file name will be.
name_of_file = input("Enter what you want the name of the file to be.")

#Then the file is created using the user input.
newfile = open(name_of_file + ".txt","w")

#Then information is written to the file.
newfile.write("It worked!")

#Then the file is closed.
newfile.close()

Upvotes: 0

chheplo
chheplo

Reputation: 211

if you have

data=['abc','bcd']

you can do

file = open('{0}.txt'.format(data[0]),"w")

which will create file as abc.txt

Write some text to a file

file.writelines('xyz')

file.close()

Upvotes: 4

User
User

Reputation: 14863

Would that do it?

n_facebook = {'christian': [22, 34]}
n_myspace = {'christian': [22, 34, 33]}

for network in globals():
    if network.startswith('n_'):
        # here we got a network
        # we save it in a file ending with _network.txt without n_ in beginning
        file(network[2:] + '_network.txt', 'w').write(str(globals()[network]))

this file saves n_facebook to facebook_network.txt. myspace also.

Upvotes: 0

Hamoudaq
Hamoudaq

Reputation: 1498

i don't understand you , cause your question not very clear ,, anyway i'll post two solution

in case you want the file name to named as the name of variable

i'd suggest using this code

for i in locals():
  if 'variable name' is i:IObject = open(i+".txt",'w')#i've added .txt extension in case you want it text file 
IObject.write("Hello i'm file name.named with the same name of variable")

or otherwise

name_of_file = raw_input("Enter the name")
IOjbect = open(name_of_file+".txt","w")
IObject.write("Hey There")

Upvotes: 1

Mike S.
Mike S.

Reputation: 4879

You can declare a value like the following:

# Let's create a file and write it to disk.
filename = "facebook.txt"

# Create a file object:
# in "write" mode
FILE = open(filename,"w")

# Write all the lines at once:
FILE.writelines("Some content goes here")

# Close
FILE.close()

Upvotes: 9

Related Questions