I'm new to python, and I'm trying to figure out how to write a program that prompts the user for the name of a text file, converts the contents of the text file to all caps, and then saves it as a new file.
Asked
Active
Viewed 2,103 times
-5
-
Did you try Googling some of the words and phrases in your question's title? – TigerhawkT3 Jun 29 '15 at 21:43
2 Answers
0
import os
def main():
fp = raw_input('Filename: ')
if fp and os.path.isfile(fp):
with open(fp, 'r') as f:
txt = f.read()
newfp = '{0}_upper{1}'.format(*os.path.splitext(fp))
with open(newfp, 'w') as f:
f.write(txt.upper())
if __name__ == '__main__':
main()

Brendan Abel
- 35,343
- 14
- 88
- 118
-
Thanks for the response. I'm getting the Name Error "raw input is not defined" – NickyNick321 Jun 29 '15 at 22:04
-
You must be using python3. In python3, `raw_input` was renamed to `input` – Brendan Abel Jun 30 '15 at 18:13
0
Using Python 2.7.6 this works for me:
filename = raw_input("File Name: ")
with open(filename, 'r+') as f:
text = f.read()
f.seek(0)
f.write(text.upper())
f.truncate()

Njord
- 142
- 6
-
Thanks for the response. I'm getting the Name Error "raw input is not defined" – NickyNick321 Jun 29 '15 at 22:04
-
@NickyNick321 What version of python are you using? Try running `python -V` (Capital V). If **you are using python 3, try the same code but `input` instead of `raw_input`**. And you should be good to go. – Njord Jun 29 '15 at 22:11