完成计算后,如何使用Shutil自己制作python文件副本? [英] How do I use shutil to make a python file copy itself after doing a calculation?
问题描述
因此,我有一个将质数写入csv文件的python程序.现在,我希望我的python文件在运行所有命令后对其自身进行复制,以作为我调整原始文件时的备份.
So I have a python program that writes prime numbers into a csv file. I now want my python file to make a copy of itself after it runs everything, as a backup for when I tweak around with the original file.
通过使python程序创建一个新的.py文件然后在其中写入所有内容,到目前为止我还不了解这一点,但这效率极低.我希望它更干净,更简单.
I've gotten away with not knowing this so far by making the python program make a new .py file and then writing everything inside it, but this is super inefficient. I'd like it to be cleaner and less complicated.
我听说过一些关于使用shutdownil.copyfile的信息,但是我不太确定如何使用它.shutil.copyfile是使自己的python文件复制自身的最佳方法,还是有更好的方法?
I've heard a little bit about using shutil.copyfile, but I'm not too sure about how to use it. Is shutil.copyfile the best way to make my python file copy itself, or is there a better way?
推荐答案
版本控制软件旨在解决此问题,即保留旧文件的副本.按照自己的方式进行操作将使您的源文件夹非常混乱.Mercurial/Hg 非常容易学习和使用(它也是用python编写的).
Revision control software is meant to solve this problem, that is, keeping a copy of your old files. Doing it your way will make your source folder really cluttered. Mercurial/Hg is really easy to learn and use (its also written in python).
否则,回答问题:
shutil.copy(__file__, "new_destination.py")
这篇关于完成计算后,如何使用Shutil自己制作python文件副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!