Python's powerful renaming method under OS module renames

Source: Internet
Author: User
Tags mkdir in python

There are a lot of powerful modules in Python, where we often use OS modules, OS modules provide more than 200 methods for us to use, and these methods are related to data processing, and here's how to rename this method.

Os renaming method is Os.rename, I use the Ipython, this thing is very powerful, just press the TAB key, can help us automatically alignment and list the available methods, found that there are 2 methods, respectively, rename and renames,2 method, The front of the rename used countless times, but the back of the renames has not been used, today is free, want to see the end has and usefulness---as the name suggests, I think it may be batch modification, haha, we look at the following demo effect:

[Root@localhost ~]# Ipython into the Ipython tool Python 2.6 (r26:66714, Dec, 11:17:00) Type "copyright", "cred   
       
Its ' or ' license ' for the more information.   
IPython 0.10.1--an enhanced Interactive Python.         ?   
-> Introduction and Overview of IPython ' s features.   
%quickref-> Quick Reference.   
Help-> the Python ' s own Help system.   Object? -> Details about ' object '. ? object also works,??   
       
Prints more. In [1]: Import OS imports OS module in [2]: Os.mkdir ("/tmp/test") new directory in [3]: Os.lis Tdir ("/tmp/test") out[3]: [] in [4]: Os.mkdir ("/tmp/test/test1") new 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 effect os.rename os.renames in [7]: Os.rename ("/tmp/test/test1", "/tm P/test/test2 ") Rename directory name in [8]: Os.listdir ("/tmp/test/") modified successfully out[8]: [' test2 '] in [9]: Os.rename The effect of automatically aligning with the TAB key Os.re Name Os.renames in [9]: Os.rename ("/tmp/test/test2", "/tmp/test1/test3") everybody notice the difference between this command and the command above, and I'm not just going to                                   Renames the last directory name, even the previous directory is renamed---------------------------------------------------------------------------OSError   
       
Traceback (most recent)/root/<ipython console> in <module> () OSError: [Errno 2] No such file or directory Hey, the error, said there is no this directory, the following is a powerful way to see below, haha in [ten]: OS.   
       
Renames ("/tmp/test/test2", "/tmp/test1/test3") in [one]: Os.listdir ("/tmp/test1/test3") out[11]: []   
Hey, incredibly successful, really powerful ah, subdirectory can be changed, the superior list can also be changed, this way may be helpful to our work. in [[]: Os.listdir ("/tmp/test1") out[12]: [' test3 ']

Similarly, he can modify the name of the directory, do not know whether the file name, the directory name to modify together, the following we look at the effect:

in [[]: Os.listdir ("/tmp/test1/test3/")   
out[22]: [' Test ']              create a new empty file in   
       
[]: Cat/tmp/test1/test3/test   
       
in [[): Os.renam   
os.rename   os.renames      
       
in []:   
os.renames ("/tmp/test1/test3/test", "/tmp/ Test/test2/test3 ")   
       
batch modification file name and directory name, hehe, sure enough, also succeeded. In   
       
[[]: Os.listdir ("/tmp/test/test2/test3")   
-------------------------------------------------------- -------------------
oserror                                   traceback (most recent)   
       
/root/<ipython console> in < Module> ()   
       
oserror: [Errno] not directory: '/tmp/test/test2/test3 ' in
       
[[]: Os.listdir ("/tmp/test/ test2/")    browsing effect   
out[26]: [' test3 '] in   
       
[27]:

I hope this article can be helpful to everyone's work and study, if interested, you can go to the test.

This article from "You are a passer-by or fearless" blog, please be sure to keep this source http://world77.blog.51cto.com/414605/561190

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.