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.

jkd
  • 1,045
  • 1
  • 11
  • 27
dhenness
  • 23
  • 1
  • 4
  • In addition to the above link, take a look at https://docs.python.org/2/library/sys.html#sys.argv and https://docs.python.org/2/library/os.html#os.rename – AMacK Apr 29 '15 at 02:41

2 Answers2

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")
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
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
dscripka
  • 51
  • 4
  • I believe you could even do `mv *.log *.mdn` (+1) – Joran Beasley Apr 29 '15 at 02:46
  • If you want to rename all files in the current directory, yes. – dscripka Apr 29 '15 at 02:49
  • 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. – dhenness Apr 29 '15 at 02:51
  • @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. – user2357112 Apr 29 '15 at 03:13