3

I've got a python script that selectively renames files in the current working directory into a certain format. I could just copy this script into each folder whenever I need to rename files but this seems cumbersome. I would like to put the script in my python scripts folder (which I've included in my path environment variable so that I can run the script from any directory) and then just call the script from the command line while the command line is navigated to directory with the files to be renamed. I could manually pass a command line argument with the path to the directory containing the files to be renamed but again this seems cumbersome and I would like to try to get around it. The command line should look like the following:

C:\Users\nheme\...\folder_that_contains_files_to_be_renamed> rename.py

Essentially, I need a way to pass the current directory of the command line to the python script. Is this possible?

2 Answers 2

3

You could

import os
print os.getcwd()
Sign up to request clarification or add additional context in comments.

2 Comments

won't this just return the directory that the python script itself is located in?
Nope, it should give you the directory you're working in on the command line
0

I don't quite understand what you mean. If you just want to pass arguments to the python script, it is very simple.

In python script:

import sys
...
path1 = sys.argv[1]
path2 = sys.argv[2]
path3 = sys.argv[3]
...

In commandline:

> rename.py your_target_path1 your_target_path2 your_target_path3

If you want to keep the python script, maybe a shell script is needed to help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.