2

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.

  • 1
    Well, where are you writing out the content to the file? You are just opening it in `wb` mode, right? – SexyBeast Jan 11 '13 at 15:10

3 Answers3

8

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())
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • For Python 3, change `"wb"` to `"w"`. Reference: [TypeError: a bytes-like object is required, not 'str' in python and CSV](https://stackoverflow.com/questions/34283178/typeerror-a-bytes-like-object-is-required-not-str-in-python-and-csv) – datalifenyc Jan 15 '19 at 18:27
1
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()
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Ofir
  • 8,194
  • 2
  • 29
  • 44
0
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())
nehem
  • 12,775
  • 6
  • 58
  • 84