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 fun开发者_Go百科ctions 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.
In general I'd say use the python libraries where possible - that way it'll be more portable, e.g. you won't need to worry about different commands or command options on various systems, and also if you need to change anything it's easier to do just python code.
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.
精彩评论