Shivani Shivani - 1 year ago 75
Python Question

How do I read the name of a text file as a String in Python?

I have a text file named doc123.txt, I want to create files named sub_doc123.txt. The file doc_123.txt is an input for a function.

Thanks in advance!

Edit: Sorry if this is basic and I am unable to see the quick solution. Essentially:

def test(main_txt_file):
print main_txt_file


This does not work as it says "NameError: name 'practice_split_3' is not defined".

Answer Source

You dont need a function for this. Following code will do.

inp = raw_input("Enter name of doc")
outName = 'sub_' + inp
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download