Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to write a piece of code which will create a text file (which the user can create the name), and write it.

It definitely needs to be a text file. It is in the exam criteria.

Python needs to be able to open the file as the code that I already have saves a file which can't be opened!

filename = input(str("what is the new file going to be called?"))
file = open(filename,'w')
file.close()
share|improve this question

put on hold as unclear what you're asking by EdChum, tobias_k, Peter Wood, LittleBobbyTables, matsjoyce 9 hours ago

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
And what is the issue you are facing? –  Lee White 18 hours ago
    
Create a small non-working example of the problem. –  Peter Wood 18 hours ago
    
it creates a file which cannot be opened by neither python, nor myself! though, it may just be my school computers –  Sam Mason 18 hours ago
    
You are asking us for help, but we have no idea what you have done, what code you have so far, and which possible solutions you've tried so far. Can you please give us some more information? –  Lee White 18 hours ago
    
try to rename the file in a file with .txt ending –  Maximilian Kindshofer 18 hours ago

3 Answers 3

up vote 1 down vote accepted

Your code should work. However, you're probably not putting a .txt extension when asked for an input. So try this:

filename = str(input("what is the new file going to be called?")) #Enter "test"
file = open(filename + '.txt','w')                                #This will add .txt 
file.close()
share|improve this answer
    
I can't thank you enough for this, it works a charm ;D –  Sam Mason 17 hours ago

I didn't much understand what you are saying.But if you want to open a new file if not exist.You should try this.

filename = input(str("what is the new file going to be called?"))
f = open(filename,'w')
f.write("something i want to print")
f.close()
f = open(filename,'r')
>>>print(f.read())
something i want to print
share|improve this answer
    
I think he struggles with the file cause it doens't have a .txt extension –  Maximilian Kindshofer 18 hours ago
    
@ Maximilian Kindshofer updated :) –  itzmeontv 17 hours ago
    
As the accepted answer show, OP doesn't know what he is doing - but who is ;) –  Maximilian Kindshofer 17 hours ago

Have you tried this?

filename = raw_input('what is the new file going to be called?')+'.txt'
file = open(filename,'w')
file.close()

or tell us what you've already tried

share|improve this answer
    
this is similar to what I have done, but it does not create a text file –  Sam Mason 18 hours ago
    
@SamMason filename = raw_input('what is the new file going to be called?')+'.txt' try this –  Aleksander Monk 18 hours ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.