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

Open source summer | File and directory operations | Multi-process and multi-threading [python advanced articles]

編輯:Python
< h2> create a directory < / h2>< img SRC="/ / img.inotgo.com/imagesLocal/202208/04/202208041549494839_1.jpg" Alt=null loading=lazy>< br>< div> operating system.MakeDir can recursively create the directory structure, such as the import operation system < / div>< br>< code> makedirs (TMP/python/fileop, exist_ok = True) < br />< / code>< br>< div> will create TMP directory in the current working directory, create a python in TMP directory directory, in python directory to create fileop directory exist_ & have spentOk = true if you want to create the directory already exists, do not report any error < / div>< h2> to delete a file or directory < / h2>< div> operating system.Remove can delete files, such as < / div>< br>< code> OS. Remove (' SDF. Py) < br />< / code>< br>< div> Rmtree () can all subdirectories and the recursive delete the directory file, such as < / div>< br>< code> Rmtree (TMP, ignore_errors = True) < br />< / code>< br>< div> note: the parameter < / div>< div>< code> ignore_ & have spentErrors = true < / code>< / div>< div> make sure it won't throw exceptions in the directory is not empty.Many of copying files shutil module directory file operation function to copy the file, you can use shutil module used by copyfile function.For example from shutil import copy files < / div>< br>< code> # d: / tools/first Py is copied to the e: / first. Py < br /> used by copyfile (' d: / tools/first. P y ', 'e: / first. P y') < br />< / code>< br>< div> please note that if before copying, e: / first Py already exists, it will be covered by the copy, so must be careful when using this function.< / div>< h2> copy directory < / h2>< div> if we want to a directory of all content (including subdirectories and files and subdirectories of subdirectories and documents, etc.) to another directory, we can use shutil copytree function.As shown below from shutil import copytree < / div>< br>< code> # d: / tools/aaa all content is copied to the directory e: / BBB < br /> copytree (' d: / tools/aaa ', 'e: / new/BBB') < br />< / code>< br>< div> please note that prior to copy the target directory may not exist, or it will report an error.Before executing the above code, if e: / new/BBB has been in existence, when performing copytree will report an error before executing the above code, if the directory e: / new does not exist, then when performing copytree, it will create the directory e: / new, and then create the directory e: / new/BBB, then put the directory d: / tools/aaa all content is copied to the e: / new/BBB.Before executing the above code, if the directory e: / new existence, but e: / new/BBB does not exist, then when performing copytree, will only create e: / new/BBB, then put the directory d: / tools/aaa all the content is copied to the e: / new/BBB.< / div>< br>< code> import OS < br />< br /> # & have spentModify the directory name & have spentD: / tools/aaa For & have spentD: / tools/BBB < br /> OS. Rename (' d: / tools/aaa ', 'd: / tools/BBB') < br />< br /> # & have spentModify the file name & have spentD: / tools/first. Py For & have spentD: / tools/second. Py. < br /> OS rename (' d: / tools/first. P y ', 'd: / tools/second. Py) < br />< / code>< h2>< / h2> to the operation of the file path name < div> for file operations, such as access to the file name, file directory and file path of stitching, can use os The Path module.In general, the way we like to use the format string to splice file path, but if your program needs to run on multiple platforms (such as Linux and Windows), then their path separators are different.Is \ on Windows, and on Linux is /.In this case, we should use OS path module.It can automatic processing the data, such as CSV data/data and data \ data file path difference, such as CSV.For example: < / div>< br>< code> & gt;& gt;& gt; Import OS < br /> & gt;& gt;& gt; Path = & have spent'/ Users/beazley/Data/Data. The CSV' < br />< br /> & gt;& gt;& gt; # & have spentGet the filename from a path section < br /> & gt;& gt;& gt; OS. The path. The basename (path) < br /> 'data. CSV' < br />< br /> & gt;& gt;& gt; # & have spentAccess path of directory < br /> & gt;& gt;& gt; OS. The path. The dirname (path) < br /> '/ Users/beazley/Data' < br />< br /> & gt;& gt;& gt; # & have spentThe file path of the splicing < br /> & gt;& gt;& gt; OS. Path. Join (TMP, & have spent'data', & have spentOS. The path. The basename (path)) < br /> 'TMP/data/data. The CSV' < br />< / code>< h2> the concept of process and thread < / h2>< div> I often asked about the difference between process and thread.Simply put: process is a running program.We write a python program (or other applications, such as brush, QQ, etc.) at run time is called a process to open the Windows task manager, which shows the current running process on the system.< / div>< br>< img SRC="/ / img.inotgo.com/imagesLocal/202208/04/202208041549494839_0.png" Alt=null loading=lazy>< br>< div>, for example, in the following program is run, only a single thread, namely, the main thread.In the main thread, the code to execute sequentially, the main thread exits, till the end.At the same time, this process is over.< / div>< br>< code> fee = Input (' please enter the lunch fee:) < br /> members = Input (' comma, please enter dinner name to English space: ') < br />< br /> # & have spentPut people in a list < br /> memberlist = Members. The split (', ') & have spent< br /> # & have spentGet the number of < br /> headcount = Len (memberlist) & have spent< br />< br /> # & have spentTo calculate the costs per capita < br /> avgfee = Int (fee) & have spent/ & have spentHeadcount < br /> print (avgfee) < br />< / code>< br>< div> we can see that our system running in many processes, such as QQ, sogou input method, etc.Before running the program, the program code files stored on disk, namely extension. Exe file.Double-click on them, and then click it.Exe file will be loaded into the memory by the operating system, operation system and become the process of each process contains at least one < / div>< div> thread < / div>< div>.A thread created by the operating system.Each thread corresponding to the data structure of code execution, it saves important status information during code execution.No thread, the operating system cannot manage and maintain code status information of the operation.Therefore, before creating threads, the operating system will not carry out our code.Although before we write a python program did not create a thread in the code, but in fact, when the python interpreter (be) the program runs, the operating system will automatically create a single thread, often refer to give priority to the thread, and the main thread of execution code instructions.When our Python interpreter implementation code.Our code is interpretation and implementation in the main thread.
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved