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
-
-
2 1684 bvdet 2,851
Recognized Expert Moderator Specialist
Source code is ascii text in a disc file. Why not read the text from the file? As in: - fn = 'file_read_code.py'
-
-
f = open('code.txt', 'w')
-
f.write(open(fn).read())
-
f.close()
-
dwblas 626
Recognized Expert Contributor
You write the info to the file and probably then change the file to executable if on a linux system. So in your example, you would write each item in the list to the file, followed by a newline. Try it with a simple "hello world" type program as a test and if there are problems, post back with a question. But there are very likely much simpler ways to do this so post back with a description of what you would like to accomplish.
Those python files I want to fill in with the text, which is python code.
Why can't you send job info or whatever to a common function along with the directory/path and process it that way. Again, try a simple example program.
Two small things - #make submit.py file
-
s = open('/submit.py','w')
-
s.close()
-
-
s = open('/compile.py','w')
You don't have to "create" the file first. You can just open [s = open('/compile.py','w' )] and write to it.
It is almost always better to use the absolute path, as chdir can cause debugging headaches. Also - f.write('cd '+ directory_name + '/' + dirName+ '\n')
-
#
-
# os.path.join solves the path joining problems.
-
'cd os.path.join(directory_name, dirName, some_other_name) \n'
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: rkoida |
last post by:
Hello All
I am working on a Problem to convert makefile in to a python script.
Are there any Modules? Please try to comment.
Thanks
rkoida
|
by: Kenneth McDonald |
last post by:
I'm not trying to persuade my company to offer Python as a scripting
language for their product, but I am trying to give them examples of
things that Python can do easily that cannot be done easily with
their current proprietary scripting language. After that it would be
their decision. As the product is a 3D package, I'm looking for
something in this field.
This does _not_ have to use PyOpenGL, or for that matter, any Python
3D...
|
by: Pankaj |
last post by:
The module which i am creating is like
Part A:
1. It does some processing by using python code.
2. The result of this python code execution is written to a text file.
]
Part B:
1. I read a text file which is outputted by above python script in a
C++ program
|
by: tatamata |
last post by:
Hello.
How can I run some Python script within C# program?
Thanks,
Zlatko
|
by: Shuaib |
last post by:
Hi!
I have a python script which returns an Integer value. How do I call
this script from a C programe, and use the result returned?
Thanks for your time.
| |
by: flit |
last post by:
Hello All,
I am trying to get information from a form and send it to a python
script without success..
Here is my objective:
User enters data in form --form send variables to python script -->
script runs and output result.
the form code
|
by: johnny |
last post by:
I have python script does ftp download in a multi threaded way. Each
thread downloads a file, close the file, calls the comman line to
convert the .doc to pdf. Command line should go ahead and convert the
file. My question is, when each thread calls the command line, does one
command line process all the request, or each thread creates a one
command line process for themselves and executes the command? For some
reason I am getting "File...
|
by: hlubenow |
last post by:
Hi,
recently there was a thread about hiding the python-script from the user.
The OP could use
http://freshmeat.net/projects/pyobfuscate/
H.
|
by: geoffbache |
last post by:
Hi all,
I have a small python script that doesn't depend on anything except
the standard interpreter. I would like to convert it to a small .exe
file on Windows that can distributed alone without introducing
additional dependencies. I need to assume, because of other python
scripts, that anyone using this has python installed anyway so I hoped
it would be possible to do this. (Why I want to do this is a bit
involved but I can explain if...
|
by: Siegfried Heintze |
last post by:
Does someone have a little python script that will read a file in
UTF-8/UTF-16/UTF-32 (my choice) and search for all the characters between
0x7f-0xffffff and convert them to an ASCII digit string that begins with
"&#" and ends with ";" and output the whole thing? If not, could someone
tell me how to write one?
How about a script to do the inverse?
Thanks!
siegfried
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |