1

I'm trying to make a program that will take a file, say my_test_file.log and make a new file called my_test_file.mdn. I'd like to be able to use this program by typing python renameprogram.py my_test_file.log into the command line. The original file will always end in .log.

1

2 Answers 2

2
from shutil import copyfile
from glob import glob
map(lambda x:copyfile(x,x[:-3]+"mdn"),glob("*.log"))

or perhaps more simply

...
import sys
copyfile(sys.argv[1],sys.argv[1][:-3]+"mdn")
Sign up to request clarification or add additional context in comments.

Comments

1

You certainly can create a Python program that will accomplish this, but there are shell level commands that already do this.

For Linux/Unix:

mv my_test_file.log my_test_file.mdn

For Windows (see this link):

rename my_test_file.log my_test_file.mdn

4 Comments

I believe you could even do mv *.log *.mdn (+1)
If you want to rename all files in the current directory, yes.
I know I can do this with unix, but the reason I'd like to do it in python is because I need to get a bunch of other data from the original file and put it into the new file in a different format I'd like to do this all with one program if possible, but that might be beyond my skills at the moment haha. This is just the first step.
@JoranBeasley: No, that wouldn't be the correct syntax. The *.mdn wouldn't expand to what you want it to expand to, and mv would complain about the target not being a directory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.