python module under os powerful renaming method renames explained in detail

  • 2020-05-27 05:57:46
  • OfStack

python module under os powerful renaming method renames explained in detail

There are many powerful modules in python, among which we often use OS module, OS module provides more than 200 methods for us to use, and these methods are related to data processing. Here is the rename method.

OS rename method is os rename, I use ipython, this thing is very strong, as long as the press TAB key, can help us to automatic alignment, and list the methods you can use, found that there are two methods, respectively is rename and renames, two methods, the previous rename used countless times, but the back of the renames haven't used, are free today, want to see how and use - as the name implies, I think may be bulk changes, ha ha, we'll look at the demonstration effect below:


[root@localhost ~]# ipython     Enter the ipython tool 
Python 2.6 (r26:66714, Dec 17 2010, 11:17:00)
Type "copyright", "credits" or "license" for more information.

IPython 0.10.1 -- An enhanced Interactive Python.
?     -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help   -> Python's own help system.
object?  -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import os           The import OS The module 

In [2]: os.mkdir("/tmp/test")     The new directory 

In [3]: os.listdir("/tmp/test")   
Out[3]: []

In [4]: os.mkdir("/tmp/test/test1")        The new directory 

In [5]: os.listdir("/tmp/test/")
Out[5]: ['test1']

In [6]: os.listdir("/tmp/test/test1")
Out[6]: []

In [7]: os.ren           According to the Tab Key auto alignment effect 
os.rename  os.renames

In [7]: os.rename("/tmp/test/test1","/tmp/test/test2")

 Rename the directory name 

In [8]: os.listdir("/tmp/test/")   Modify the success 
Out[8]: ['test2']

In [9]: os.rename          According to the Tab Key auto alignment effect 
os.rename  os.renames

In [9]: os.rename("/tmp/test/test2","/tmp/test1/test3")

 Notice the difference between this command and the one above. I'm not just renaming the last directory, I'm renaming the previous directory as well 
---------------------------------------------------------------------------
OSError                  Traceback (most recent call last)

/root/<ipython console> in <module>()

OSError: [Errno 2] No such file or directory

 Hey hey, wrong, said there is no this directory, 
 Take a look at this powerful method below, haha 

In [10]: os.renames("/tmp/test/test2","/tmp/test1/test3")

In [11]: os.listdir("/tmp/test1/test3")
Out[11]: []

 Hey hey, incredibly successful, is really strong ah, subdirectory can change, the directory can also change, this method may be helpful to our work. 
In [12]: os.listdir("/tmp/test1")
Out[12]: ['test3']

 

Similarly, he can modify the directory name, I do not know if it can be attached to the file name, directory name 1 from the modification, let's look at the effect:


In [22]: os.listdir("/tmp/test1/test3/")
Out[22]: ['test']        new 1 An empty file 

In [23]: cat /tmp/test1/test3/test

In [24]: os.renam
os.rename  os.renames  

In [24]:
os.renames("/tmp/test1/test3/test","/tmp/test/test2/test3")

 Batch modification file name and directory name, hey hey, and it was successful. 

In [25]: os.listdir("/tmp/test/test2/test3")
---------------------------------------------------------------------------
OSError                  Traceback (most recent call last)

/root/<ipython console> in <module>()

OSError: [Errno 20] Not a directory: '/tmp/test/test2/test3'

In [26]: os.listdir("/tmp/test/test2/")   Browse the effect 
Out[26]: ['test3']

In [27]:

I hope this article can be helpful to your work and study. If you are interested, you can try it on your own.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: