Shivani Shivani - 4 months ago 8
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

test(practice_split_3.txt)


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

Answer

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

inp = raw_input("Enter name of doc")
outName = 'sub_' + inp
Comments