Create file with variable name 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!

asked Jul 26, 2012 at 19:14

NumberOneRobotNumberOneRobot

1,5955 gold badges16 silver badges23 bronze badges

3

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[]

answered Jul 26, 2012 at 19:18

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[]

answered Jul 26, 2012 at 19:18

chheplochheplo

2113 silver badges14 bronze badges

1

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"]

answered Jul 26, 2012 at 19:46

HamoudaqHamoudaq

1,4484 gold badges23 silver badges42 bronze badges

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.

answered Jul 26, 2012 at 20:11

UserUser

13.7k1 gold badge36 silver badges59 bronze badges

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[]

answered Aug 8, 2018 at 13:43

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[]

divibisan

10.6k11 gold badges39 silver badges57 bronze badges

answered Nov 15, 2014 at 1:34

How do you create a filename in a variable in Python?

Use string formatting to create a filename using variables.
name = "test_file".
filename = "%s.csv" % name..
print[filename].

How do you name a text file in Python?

Use rename[] method of an OS module rename[] method to rename a file in a folder. Pass both the old name and a new name to the os. rename[old_name, new_name] function to rename a file.

What does __ file __ mean in Python?

When a module is loaded from a file in Python, __file__ is set to its path. You can then use that with other functions to find the directory that the file is located in.

How do I write multiple values in a Python file?

python write multiple variables to file..
r – read mode..
a – append mode..
w – writing mode..
r+ – both read and write mode..

Chủ Đề