Python – How do I run a Python script in the current command line directory?

How do I run a Python script in the current command line directory?… here is a solution to the problem.

How do I run a Python script in the current command line directory?

I have a python script that can selectively rename files in the current working directory to some format. Whenever I need to rename a file, I can copy this script to every folder, but it looks like a hassle. I want to put the script in my python scripts folder (I’ve included it in my path environment variable so I can run the script from any directory) and then invoke the script from the command line when navigating to the directory containing the file you want to rename. I can manually pass a command line argument with the path to the directory where the file to be renamed is located, but this again seems cumbersome and I want to try to bypass it. The command line should look like this:

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?

Solution

You can

import os
print os.getcwd()

Related Problems and Solutions