Python: Convert contents of a file to Uppercase characters only

后端 未结 3 790
盖世英雄少女心
盖世英雄少女心 2021-01-22 02:28

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


        
3条回答
  •  轮回少年
    2021-01-22 02:58

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

提交回复
热议问题