I have an environment called doors
and I would like to rename it to django
for the virtualenvwrapper.
I"ve noticed that if I just rename the folder ~/.virtualenvs/doors
to django
, I can now call workon django
, but the environment still says (doors)[email protected]
.
Rename an environment with virtualenvwrapper rename: Questions
Rename a dictionary key
5 answers
Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value?
In case of OrderedDict, do the same, while keeping that key"s position.
Answer #1
For a regular dict, you can use:
mydict[k_new] = mydict.pop(k_old)
This will move the item to the end of the dict, unless k_new
was already existing in which case it will overwrite the value in-place.
For a Python 3.7+ dict where you additionally want to preserve the ordering, the simplest is to rebuild an entirely new instance. For example, renaming key 2
to "two"
:
>>> d = {0:0, 1:1, 2:2, 3:3}
>>> {"two" if k == 2 else k:v for k,v in d.items()}
{0: 0, 1: 1, "two": 2, 3: 3}
The same is true for an OrderedDict
, where you can"t use dict comprehension syntax, but you can use a generator expression:
OrderedDict((k_new if k == k_old else k, v) for k, v in od.items())
Modifying the key itself, as the question asks for, is impractical because keys are hashable which usually implies they"re immutable and can"t be modified.
How to rename a file using Python
5 answers
I want to change a.txt
to b.kml
.
Answer #1
Use os.rename
:
import os
os.rename("a.txt", "b.kml")
Rename multiple files in a directory in Python
5 answers
I"m trying to rename some files in a directory using Python.
Say I have a file called CHEESE_CHEESE_TYPE.***
and want to remove CHEESE_
so my resulting filename would be CHEESE_TYPE
I"m trying to use the os.path.split
but it"s not working properly. I have also considered using string manipulations, but have not been successful with that either.
Answer #1
Use os.rename(src, dst)
to rename or move a file or a directory.
$ ls
cheese_cheese_type.bar cheese_cheese_type.foo
$ python
>>> import os
>>> for filename in os.listdir("."):
... if filename.startswith("cheese_"):
... os.rename(filename, filename[7:])
...
>>>
$ ls
cheese_type.bar cheese_type.foo