程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

File and directory operations | multi-process and multi-threading [python advanced articles]

編輯:Python

Article table of contents

  • Create a directory
  • Deleting a file or directory
  • Copy Directory
  • Operations on file pathnames
  • The concept of process and thread

Create directory

Operating system.MakeDir can recursively create directory structures, e.g.
import OS

makedirs('tmp/python/fileop', exist_ok=True)

The TMP directory will be created under the current working directory, the python directory will be created under the TMP directory, and the fileop directory will be created under the python directory
exist_ Ok=true specifies that if the directory to be created already exists, no errors will be reported

Deleting a file or directory

Operating system.Remove can delete files, such as

os.remove('sdf.py')

Rmtree() can recursively delete all subdirectories and subfiles of a directory, e.g.

rmtree('tmp', ignore_errors=True)

Note: The parameter ignore_Errors=true ensures that no exception is thrown if the directory is not empty.
Copying files
There are many directory file manipulation functions in the shutil module
To copy files, you can use the copyfile function of the shutil module.
eg
import copy file from shutil

#Copy d:/tools/first Py to e:/first.Pycopyfile('d:/tools/first.py', 'e:/first.py')

Note that if e:/first Py already exists before copying, it will be overwritten by copy, so care must be taken when using this function.

Replica Directory

If we want to copy all the contents in one directory (including subdirectories and files, subdirectories and documents in subdirectories, etc.) to another directory, we can use shutil's copytree function.
as follows
import copytree from shutil

#Copy everything in the d:/tools/aaa directory to e:/bbbcopytree('d:/tools/aaa', 'e:/new/bbb')

Please note that the destination directory must not exist before copying, otherwise an error will be reported.
Before executing the above code, if e:/new/bbb already exists, an error will be reported when executing copytree
Before executing the above code, if the directory e:/new does not exist, when executing copytree, it will create directory e:/new, then create directory e:/new/bbb, then copy all contents of directory d:/tools/aaa to e:/new/bbb.
Before executing the above code, if the directory e:/new exists, but e:/new/bbb does not exist, then when copytree is executed, only e:/new/bbb will be created, and then the directory d:/toolsCopy everything in /aaa to e:/new/bbb.

import os# Modify the directory name d:/tools/aaa to d:/tools/bbbos.rename('d:/tools/aaa','d:/tools/bbb')# Modify the file name d:/tools/first.py to d:/tools/second.pyos.rename('d:/tools/first.py','d:/tools/second.py')

Operations on file pathnames

For file name operations, such as obtaining the file name, the directory where the file is located, and the concatenation of the file path, you can use the os Path module.
Generally, we like to use the format string method to concatenate file paths, but if your program needs to run on multiple platforms (such as Linux and Windows), the delimiters of their paths are different.\ on windows, and / on linux.
In this case, we should use the os path module.It can automatically handle data like data/data CSV and data\data file path differences like CSV.
For example:

>>> import os>>> path = '/Users/beazley/Data/data.csv'>>> # Get the filename part of the path>>> os.path.basename(path)'data.csv'>>> # Get the directory part of the path>>> os.path.dirname(path)'/Users/beazley/Data'>>> # concatenation of file paths>>> os.path.join('tmp', 'data', os.path.basename(path))'tmp/data/data.csv'

The concept of processes and threads

I am often asked about the difference between a process and a thread.
Simply put: a process is a running program.
The python programs (or other applications, such as paintbrush, QQ, etc.) that we write are called processes when they are running
Open the task manager under windows, which displays the processes running on the current system.

For example, after the following program runs, there is only one thread, the main thread.In the main thread, the code is executed sequentially and the main thread exits until the end of execution.In the meantime, the process has come to an end.

fee = input('Please enter the cost of lunch:')members = input('Please enter the names of the dinner parties, separated by commas:')# put people into a listmemberlist = members.split(',')# get the number of peopleheadcount = len(memberlist)# Calculate per capita costavgfee = int (fee) / headcountprint(avgfee)

We can see that there are many processes running in our system, such as QQ, Sogou input method, etc.
Before running these programs, their program code files are stored on disk as files with the extension .Exe.
Double-click on them, then click on these.Exe files will be loaded into memory by the operating system, run and become a process
Every process in the system contains at least one thread.
Threads are created by the operating system.Each thread corresponds to a data structure for code execution, which holds important state information during code execution.
Without threads, the operating system cannot manage and maintain state information for code operations.
Therefore, the operating system will not execute our code until the thread is created.
Although there is no thread creation code in the python program we wrote before, in fact, when the python interpreter program runs (becomes a process), the operating system automatically creates a thread, usually called the main thread, and hereCode instructions are executed in the main thread.
When the interpreter executes our Python program code.Our code is interpreted and executed in this main thread.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved