[Tutor] String switch

Steven D'Aprano steve at pearwood.info
Tue Oct 11 03:31:17 CEST 2011


Christopher King wrote:
> Okay, there is a python file called target.py. In the same directory there
> is a file named main.py. You are the author of main.py. The code in main.py
> will write to target.py. Then the antivirus catches main.py and removes, but
> not the modification to target.py. Main.py can not create new files. What
> would Main.py have to write to target.py, so that its output is swapped
> case. It does not matter how this is achieved. I know that one way is to do
> a regex search for every string in the file and then write the text of the
> file, with swapcases after strings, back to the file. What is a more elegant
> way for this to be achieved.

What makes you think that target.py contains any strings at all? Or that 
the strings are only used for output?

Why do you say that main.py is deleted by "the anti-virus"? (Which 
anti-virus?) Has this actually happened to you, or is this a 
hypothetical question?

If you have target.py on your computer, why don't you just edit it 
yourself instead of trying to write a script to modify it?

Or better still, leave target.py alone and just pipe the output to a 
second script which runs swapcase on its input? Something like this 
should work:

# swapcase script
import sys
sys.stdout.write(sys.stdin.read().swapcase())


then from the shell:

python swapcase.py < python target.py


(All of the above is untested, but should work on Linux or Mac, and 
probably even Windows.)





-- 
Steven


More information about the Tutor mailing list