The Artima Developer Community
Sponsored Link

Python Answers Forum
How to convert python script to the text?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Alex Ivanov

Posts: 1
Nickname: krystofer
Registered: Aug, 2010

How to convert python script to the text? Posted: Aug 12, 2010 1:57 AM
Reply to this message Reply
Advertisement
Hi everybody!
I want automatically create many folders with the python files inside. Those python files I want to fill in with the text, which is python code. So I want to insert that code as a text. The problem is that the code contains commands which are not recognizable as a text. Can you tell me, please, how to force python to consider code as a text.

Thanks in advance.

Here is my code:


import os
import shutil

beginh=0
beginm=0
begink=0

hr=3 # h
mr=4 # death increment due to foraging
kr=5 # perturbation max and min range difference

directory_name = 'foodPert'
for h in range(beginh, beginh+hr):
if h == 0:
d = 100
if h == 1:
d = 500
if h == 2:
d = 1000
for m in range(beginm, beginm+mr):
if m == 0:
df = 0
if m == 1:
df = 0.1
if m == 2:
df = 0.2
if m == 3:
df = 0.3
for k in range(begink, begink+kr):
if k == 0:
fp = 2
if k == 1:
fp = 4
if k == 2:
fp = 6
if k == 3:
fp = 8
if k == 4:
fp = 10
dirName = directory_name+'_h_'+ str(d) + '_df_' + str(df) + '_fp_' + str(fp)
os.mkdir(dirName)

#make compile.py file
c = open('/compile.py','w')
c.close()

c = open('/compile.py','w')
# this information I'll put inside the file
l = ['
import os
import shutil
seeds=5
beginrep=0
cpp_name = 'FoodPerturbation.cpp'
myprogram = directory_name+'_h_'+ str(d) + '_df_' + str(df) + '_fp_' + str(fp)


directory_name = directory_name+'_h_'+ str(d) + '_df_' + str(df) + '_fp_' + str(fp)

for k in range(beginrep,beginrep+seeds):

dirName = directory_name+str(k+1)

#if directory is not there yet, create it
if not (os.path.isdir('./' + dirName + '/')):
os.mkdir(dirName)
#copy necessary files
shutil.copyfile('random.h',dirName+'/random.h')
shutil.copyfile('SocialColony.h',dirName+'/SocialColony.h')
shutil.copyfile('SocialPopulation.h',dirName+'/SocialPopulation.h')
shutil.copyfile('Individual.h',dirName+'/Individual.h')
shutil.copyfile(cpp_name, dirName + '/' + cpp_name)
#change to the subdirectory
os.chdir(dirName)
#compile within subdirectory, creating the executable within subdirectory
os.popen('g++ -g ' + cpp_name + ' -o ' + myprogram)
#change back to higher directory
os.chdir('../')
#make job description files (handy if they have a keyword that distinguishes them, like 'job')
job_name='job_' + myprogram +'_s'+str(k+1)

f=open(job_name,'w')
f.write('#PBS -N dynamics'+'_rep'+str(k+1)+'\n')
f.write('#PBS -l walltime=100:00:00\n')
f.write('#PBS -l ncpus=2\n')
f.write('#PBS -j oe\n')
f.write('cd '+ directory_name + '/' + dirName+ '\n')
f.write('./' + myprogram + ' ' + str(k+1) + '\n')

f.close'
]
for i in l:
c.write(i)
c.close

#make submit.py file
s = open('/submit.py','w')
s.close()

s = open('/compile.py','w')
# this information I'll put inside the file
j = ['
import os

seeds=5
beginrep=0

for k in range(beginrep,beginrep+seeds):
job_name='deathPert_h500_df_0_pr_0.015_per_0.5'+'_s'+str(k+1)
os.popen('qsub '+'job_'+job_name)

os.chdir('..')
'
]
for i in j:
s.write(i)
s.close

Topic: GCD program using Euclid’s algorithm Previous Topic   Next Topic Topic: interrup execution

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use