本文共 5416 字,大约阅读时间需要 18 分钟。
python列重命名
Good day, learners! In this tutorial we are going to learn about Python Directory. In our previous tutorial, we learned about .
祝您学习愉快! 在本教程中,我们将学习Python目录。 在上一教程中,我们了解了 。
Python Directory is basically working with operating system directories. To work with Python Directories, we need to import os module. This os modules contains functions to create, view, delete, rename directories.
Python目录基本上可以与操作系统目录一起使用。 要使用Python目录,我们需要导入os模块。 该操作系统模块包含用于创建,查看,删除,重命名目录的功能。
Basically, there are some common functions to access Python Directories. The functions are given below
基本上,有一些通用功能可以访问Python目录。 功能如下
Using these functions, you can do basic operation with python directories. But, to show you example, some sample code will be given to help you understand about the implementation.
使用这些功能,您可以对python目录执行基本操作。 但是,为了展示示例,将提供一些示例代码来帮助您了解实现。
You can get the list of directories on a specific location. To do so, you have to to use listdir(location) function. If you put the location, the function will return a list of string containing the names of the directories of the given location. The following code will help you understand the thing
您可以获取特定位置的目录列表。 为此,您必须使用listdir(location)函数。 如果放置该位置,该函数将返回一个字符串列表,其中包含给定位置的目录名称。 以下代码将帮助您理解
import os #the os module need to be importedprint(os.listdir('/usr')) #here the location is ‘/usr’
The output of the following code will be:
以下代码的输出将是:
================== RESTART: /home/imtiaz/directory.py ==================/home/imtiaz['locale', 'sbin', 'local', 'games', 'lib', 'share', 'lib32', 'src', 'include', 'bin']>>>
Which is same as the actual picture.
与实际图片相同。
As we said earlier, you can get the location of the current directory you’re in by using getcwd() function. The following code will illustrate you the idea
如前所述,您可以使用getcwd()函数获取当前目录的位置。 以下代码将说明您的想法
import os #we need to import this moduleprint(os.getcwd()) #print the current location
The output of the following code will be
以下代码的输出将是
================== RESTART: /home/imtiaz/cur_dir.py ==================/home/imtiaz>>>
Similarly, you can implement all those functions that are mentioned above. Try those, give yourself a challenge!
同样,您可以实现上述所有这些功能。 试试看,给自己一个挑战!
Reading this tutorial, it may come to your mind that why we need Python Directories. In this section we will going to discuss this thing.
阅读本教程,您可能会想到为什么我们需要Python目录。 在本节中,我们将讨论这个问题。
Suppose, you are making some a software using Python where you need to read/write files from different directories. The directories can be dynamic so that you cannot fix the directory from your code, rather you need to choose the directory dynamically. After choosing the directory, you may have to create a new directory or write a file or read a file from that directory. To do so, Python has introduced this facility. You may not need this now, but Python Directory may help you later.
假设您正在使用Python开发一些软件,需要从不同目录读取/写入文件。 目录可以是动态的,因此您无法从代码中修复目录,而是需要动态选择目录。 选择目录后,您可能必须创建一个新目录或写入文件或从该目录读取文件。 为此,Python引入了此功能。 您现在可能不需要此功能,但是Python目录稍后可能会为您提供帮助。
A simple program showing how to create directory, then rename and delete it.
一个简单的程序,显示如何创建目录,然后重命名和删除它。
import os#change directoryos.chdir('/Users/pankaj/temp')#print current working directoryprint(os.getcwd())#create directoryos.mkdir("data")print(os.listdir(os.getcwd()))#rename directoryos.rename("data","data1")print(os.listdir(os.getcwd()))#delete directoryos.rmdir("data1")print(os.listdir(os.getcwd()))#delete non-empty directoryos.rmdir("test")print(os.listdir(os.getcwd()))
When we execute above program through terminal, it produces following output.
当我们通过终端执行上述程序时,将产生以下输出。
pankaj:BasicPython pankaj$ python directory.py /Users/pankaj/temp['data', 'test']['data1', 'test']['test']Traceback (most recent call last): File "directory.py", line 22, inos.rmdir("test")OSError: [Errno 66] Directory not empty: 'test'
Notice that os.rmdir
can only remove empty directory. So to delete a non-empty directory, we will have to use shutil
module. Below is a simple program to delete directory using shutil module.
请注意, os.rmdir
只能删除空目录。 因此,要删除一个非空目录,我们将不得不使用shutil
模块。 下面是一个使用shutil模块删除目录的简单程序。
import shutilshutil.rmtree('/Users/pankaj/temp/test')
So that’s all for python directory tutorial. Hope that you have learned well. Try to implement functions that are not implemented here. That’s you task! If you face any problem regarding this, feel free to use the comment section.
这就是python目录教程的全部内容。 希望你学得很好。 尝试实现此处未实现的功能。 那是你的任务! 如果您对此有任何疑问,请随时使用评论部分。
Reference:
参考: :
翻译自:
python列重命名
转载地址:http://gyqzd.baihongyu.com/