简体   繁体   中英

Which is better? Using inbuilt python functions or os.system commands?

Which is better to use in a python automation script for following simple operations To create a zip file and copy it or rename it to a new location. Using python inbuilt functions or terminal commands through os.system modules is better?

The inbuilt Python modules/ stdlib wherever you can, subprocess (os.system) where you must.

Reasons: Portability, maintenance, code readability just to name a few.

I'd use the Python library if possible. The biggest drawbacks of the built-in functions appear to me that they often don't support all the features of the underlying OS, for example Alternate Data Streams or Access Control Lists etc. under NTFS may get lost in a move operation.

I would say python ones since it'll make the script portable. But sometimes, performance and availability are of concerns.

一般来说,我会说尽可能使用python库 - 这样它会更便携,例如你不需要担心各种系统上的不同命令或命令选项,如果你需要更改任何东西,它也会更容易只做python代码。

Using python internals command is nice, especially in terms of portability.

But at some point, you can be confused by lack of "os.kill" in Python older than 2.7 (Windows), you can be surprised by way how os.Popen is working, than you will discover win32pipe etc etc.

Personally I would suggest always a small research (do you need daemons etc) and then decide. If you don't need windows platform - using python's internals could be more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM