user1816467
user1816467

Reputation:

Python: Convert contents of a file to Uppercase characters only

I am trying to convert the contents a set of files to only uppercase characters. Heres the code I have so far:

import os

def test():
    os.chdir("C:/Users/David/Files")
    files = os.listdir(".")
    for x in files:
        inputFile = open(x, "r")
        content = inputFile.read()
        with open(x, "wb") as outputFile:
                str.upper(content)

When I run it all it does it delete the content of the file.

Upvotes: 2

Views: 21529

Answers (3)

nehem
nehem

Reputation: 13642

import os
def test(x):
    with open(x, "r+b") as file:
        content = file.read()
        file.seek(0)
        file.write(content.upper())

The above should work if you pass the file name in x

Below is command line version to do the same for a single file & you can invoke from the terminal by ls -1 | parallel python upper.py

import os, sys
with open(sys.argv[1], "r+b") as file:
    content = file.read()
    file.seek(0)
    file.write(content.upper())

Upvotes: 0

Ofir
Ofir

Reputation: 8362

def test():
os.chdir("C:/Users/David/Files")
files = os.listdir(".")
for x in files:
    inputFile = open(x, "r")
    content = inputFile.read()
    with open(x, "wb") as outputFile:
            outputfile.write(str.upper(content))
            outputfile.close()

Upvotes: 1

RocketDonkey
RocketDonkey

Reputation: 37269

You aren't actually writing the contents of the file. Try outputFile.write(content.upper()).

import os

def test():
    os.chdir("C:/Users/David/Files")
    files = os.listdir(".")
    for x in files:
        inputFile = open(x, "r")
        content = inputFile.read()
        with open(x, "wb") as outputFile:
                outputFile.write(content.upper())

When you open a file with w, any existing file with that name gets erased (see here). While this is fine, it is your statement str.upper(content) that is causing an issue because it doesn't actually do anything to the file - it uppercases content but doesn't write it anywhere (note you can also call .upper() on the content itself if you want). In order to write the contents to the file, you take the outputFile that you created and write content to it using the write() method. You can also use another with statement as you did when writing the file, which will ensure that it also gets properly closed:

import os

def test():
    os.chdir("C:/Users/David/Files")
    files = os.listdir(".")
    for x in files:
        with open(x, "r") as inputFile:
            content = inputFile.read()
        with open(x, "wb") as outputFile:
            outputFile.write(content.upper())

You could also try opening the file in r+b mode, reading and then overwriting by seeking to the beginning of the file and writing (the file length should be the same, but you can use truncate() to clear the file after reading if need be):

import os

def test():
    x = 'testfile'
    with open(x, "r+b") as inputFile:
        content = inputFile.read()
        inputFile.seek(0)
        inputFile.write(content.upper())

Upvotes: 8

Related Questions