Best way to have a python script copy itself?

2024/10/5 21:21:17

I am using python for scientific applications. I run simulations with various parameters, my script outputs the data to an appropriate directory for that parameter set. Later I use that data. However sometimes I edit my script; in order to be able to reproduce my results if needed I would like to have a copy of whatever version of the script was used to generate the data live right in the directory with the data. So basically I would like to have my python script copy itself to the data directory. What's the best way to do this?

Thanks!

Answer

I stumbled across this question as I wanted to do the same thing. Although I agree with the comments that git/VCS with revision and everything would be the cleanest solution, sometimes you just want something quick and dirty that does the job. So if anyone is still interested:

With __file__ you can access the running scripts filename (with path), and as already suggested you can use a high-level file manipulation lib like shutil to copy it to some place. In one line:

shutil.copy(__file__, 'experiment_folder_path/copied_script_name.py') 

With the corresponding imports and some bells and whistles:

import shutil
import os     # optional: for extracting basename / creating new filepath
import time   # optional: for appending time string to copied script# generate filename with timestring
copied_script_name = time.strftime("%Y-%m-%d_%H%M") + '_' + os.path.basename(__file__)# copy script
shutil.copy(__file__, 'my_experiment_folder_path' + os.sep + copied_script_name) 
https://en.xdnf.cn/q/70438.html

Related Q&A

How to convert dictionary to matrix in python?

I have a dictionary like this:{device1 : (news1, news2, ...), device2 : (news 2, news 4, ...)...}How to convert them into a 2-D 0-1 matrix in python? Looks like this:news1 news2 news3 news4 device1 …

Ubuntu 16.04 - Why I cannot install libtiff4-dev?

Following this tutorial, I am trying to install the OpenCV 3 with Python on Ubuntu 16.04.At the step of entering $ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-devI got this me…

Histogram update in a for loop with matplotlib.pylab

I am trying to update in for loop a histogram data. but I dont know how to make it. I tried with set_data but it is not working. here is the code:plt.ion() ax=plt.subplot(111) [n,X, V]=ax.hist(range(MA…

How to remove a section from an ini file using Python ConfigParser?

I am attempting to remove a [section] from an ini file using Pythons ConfigParser library.>>> import os >>> import ConfigParser >>> os.system("cat a.ini") [a] b = c…

why does this script not work with threading python

so ive been trying to ifnd a way to access task manager. Ive tried a few methods including the wmi module and the windows tasklist but neither suit my need. wmi is way too slow and tasklist becomes to…

django-admin command not working in Mac OS

I started Django in Mac OS and after installing Django using pip, I tried to initiated a new project using the command django-admin startproject mysite. I get the error -bash: django-admin: command not…

HeartBleed python test script

I came across this Python script that tests the server for the HeartBleed vulnerability: Would someone be able to explain the content of the "hello", what is being sent and how was this cont…

How to convert a wand image object to numpy array (without OpenCV)?

I am converting pdf files to image using Wand. Then, I do further image processing using ndimage. I would like to directly convert the Wand image into a ndarray... I have seen the answer here, but it u…

Import error running unittest in Python3

I have a problem importing files in Python 3.6. My directories tree is as given below:project/app/├── __init__.py├── a.py└── b.pytest/├── __init__.py├── test_a.py└── test_b.pyIt works…

python: obtaining the OSs argv[0], not sys.argv[0]

(This question was asked here, but the answer was Linux-specific; Im running on FreeBSD and NetBSD systems which (EDIT: ordinarily) do not have /proc.)Python seems to dumb down argv[0], so you dont get…