Fix Python – How to rename a virtualenv in Python?

I misspelled the name of the virtualenv while initializing it using:
$ virtualenv vnev

I actually intended to create the environment with the name venv.
Having tried to rename the vnev folder to venv, I find that this doesn’t provide much help. The name of the activate environment still renames the old vnev.
$ mv vnev venv
$ . venv/bin/activate
(….

Fix Python – Get folder name of the file in Python

In Python what command should I use to get the name of the folder which contains the file I’m working with?
“C:\folder1\folder2\filename.xml”
Here “folder2” is what I want to get.
The only thing I’ve come up with is to use os.path.split twice:
folderName = os.path.split(os.path.split(“C:\folder1\folder2\filename.xml”)[0])[1]

Is there any better w….

Fix Python – Where are the python modules stored?

I have recently started learning Python and I have 2 questions relating to modules.

Is there a way to obtain a list of Python modules available (i.e. installed) on a machine?
I am using Ubuntu Karmic and Synaptic for package management. I have just installed a python module.Where is the module code actually stored on my machine? (is there a defau….

Fix Python – How can I extract the folder path from file path in Python?

I would like to get just the folder path from the full path to a file.
For example T:\Data\DBDesign\DBDesign_93_v141b.mdb and I would like to get just T:\Data\DBDesign (excluding the \DBDesign_93_v141b.mdb).
I have tried something like this:
existGDBPath = r’T:\Data\DBDesign\DBDesign_93_v141b.mdb’
wkspFldr = str(existGDBPath.split(‘\\’)[0:-1])
pri….

Fix Python – Iterating through directories with Python

I need to iterate through the subdirectories of a given directory and search for files. If I get a file I have to open it and change the content and replace it with my own lines.
I tried this:
import os

rootdir =’C:/Users/sid/Desktop/test’

for subdir, dirs, files in os.walk(rootdir):
for file in files:
f=open(file,’r’)
lines=….

Fix Python – How do I get the path of the current executed file in Python?

This may seem like a newbie question, but it is not. Some common approaches don’t work in all cases:
sys.argv[0]
This means using path = os.path.abspath(os.path.dirname(sys.argv[0])), but this does not work if you are running from another Python script in another directory, and this can happen in real life.
__file__
This means using path = os.path….