This article mainly introduces the powerful renames method for the OS module in python. For more information, see
Renames, a powerful renames method for the OS module in python
There are many powerful modules in python. among them, we often use the OS module. the OS module provides over 200 methods for us to use. these methods are related to data processing, here we will introduce How to rename this method.
The OS Rename method is OS. rename: ipython I used. this is a powerful tool. press the TAB key to automatically align and list available methods. I found that there are two methods, they are rename and renames, respectively. The previous rename has been used countless times, but renames has not been used yet. if you are free today, you can see whether it is useful or not-as the name suggests, I think it may be batch modification. haha. let's take a look at the demo:
[Root @ localhost ~] # Ipython go to 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 to import the OS module In [2]: OS. mkdir ("/tmp/test") creates a directory In [3]: OS. listdir ("/tmp/test") Out [3]: [] In [4]: OS. mkdir ("/tmp/test/test1") creates a directory In [5]: OS. listdir ("/tmp/test/") Out [5]: ['test1'] In [6]: OS. listdir ("/tmp/test/test1") Out [6]: [] In [7]: OS. ren press the Tab key to automatically align the OS. rename OS. renames In [7]: OS. rename ("/tmp/test/test1", "/tmp/test/test2") rename the directory name In [8]: OS. listdir ("/tmp/test/") is modified successfully. Out [8]: ['test2'] In [9]: OS. rename automatically alignment by pressing the Tab key OS. rename OS. renames In [9]: OS. rename ("/tmp/test/test2", "/tmp/test1/test3") note the differences between this command and the preceding command, I not only need to rename the last directory name, but also rename the previous directory --------------------------------------------------------------------- OSError Traceback (most recent call last)/root/
In
() OSError: [Errno 2] No such file or directory Hey, an error is reported, saying there is No such directory. let's take a look at the following powerful method. haha In [10]: OS. renames ("/tmp/test/test2", "/tmp/test1/test3") In [11]: OS. listdir ("/tmp/test1/test3") Out [11]: [] Hey hey, it's actually successful. it's really powerful. sub-directories can be changed, and parent directories can also be changed, this method may be helpful for our work. In [12]: OS. listdir ("/tmp/test1") Out [12]: ['test3']
Similarly, you can modify the directory name. if you do not know whether the file name can be connected or the directory name can be modified together, let's take a look at the effect:
In [22]: OS. listdir ("/tmp/test1/test3/") Out [22]: ['test'] create an empty file In [23]: cat/tmp/test1/test3/test In [24]: OS. renamos. rename OS. renames In [24]: OS. renames ("/tmp/test1/test3/test", "/tmp/test/test2/test3") batch modifies the file name and directory name. In [25]: OS. listdir ("/tmp/test/test2/test3") restore OSError Traceback (most recent call last)/root/
In
() OSError: [Errno 20] Not a directory: '/tmp/test/test2/test3' In [26]: OS. listdir ("/tmp/test/test2/") browsing effect Out [26]: ['test3'] In [27]:
Thank you for reading this article. I hope it will help you. thank you for your support for this site!
For more information about renames, which is a powerful renames method for the OS module in python, refer to the PHP Chinese website!