-5

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.

NickyNick321
  • 213
  • 1
  • 4
  • 12

2 Answers2

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