473,378 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to convert python script to the text?

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:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import shutil
  3.  
  4. beginh=0 
  5. beginm=0 
  6. begink=0 
  7.  
  8. hr=3 # h 
  9. mr=4 # death increment due to foraging
  10. kr=5 # perturbation max and min range difference
  11.  
  12. directory_name = 'foodPert'
  13. for h in range(beginh, beginh+hr):
  14.     if h == 0:
  15.        d = 100
  16.     if h == 1:
  17.        d = 500
  18.     if h == 2:
  19.        d = 1000
  20.     for m in range(beginm, beginm+mr):
  21.         if m == 0:
  22.            df = 0
  23.         if m == 1:
  24.            df = 0.1
  25.         if m == 2:
  26.            df = 0.2
  27.         if m == 3:
  28.            df = 0.3
  29.         for k in range(begink, begink+kr):
  30.             if k == 0:
  31.                 fp = 2
  32.             if k == 1:
  33.                 fp = 4
  34.             if k == 2:
  35.                 fp = 6
  36.             if k == 3:
  37.                 fp = 8
  38.             if k == 4:
  39.                 fp = 10
  40.             dirName = directory_name+'_h_'+ str(d) + '_df_' + str(df) + '_fp_' + str(fp) 
  41.             os.mkdir(dirName)
  42.  
  43.             #make compile.py file
  44.             c = open('/compile.py','w')
  45.             c.close()
  46.  
  47.             c = open('/compile.py','w')
  48.             # this information I'll put inside the file
  49.             l = ['
  50.                 import os
  51.                 import shutil
  52.                 seeds=5
  53.                 beginrep=0
  54.                 cpp_name = 'FoodPerturbation.cpp'
  55.                 myprogram = directory_name+'_h_'+ str(d) + '_df_' + str(df) + '_fp_' + str(fp) 
  56.  
  57.  
  58.                 directory_name = directory_name+'_h_'+ str(d) + '_df_' + str(df) + '_fp_' + str(fp) 
  59.  
  60.                 for k in range(beginrep,beginrep+seeds):
  61.  
  62.                     dirName = directory_name+str(k+1)
  63.  
  64.                 #if directory is not there yet, create it    
  65.                     if not (os.path.isdir('./' + dirName + '/')):
  66.                         os.mkdir(dirName)
  67.                 #copy necessary files    
  68.                     shutil.copyfile('random.h',dirName+'/random.h')
  69.                     shutil.copyfile('SocialColony.h',dirName+'/SocialColony.h')
  70.                     shutil.copyfile('SocialPopulation.h',dirName+'/SocialPopulation.h')
  71.                     shutil.copyfile('Individual.h',dirName+'/Individual.h')
  72.                     shutil.copyfile(cpp_name, dirName + '/' + cpp_name)    
  73.                 #change to the subdirectory
  74.                     os.chdir(dirName)
  75.                     #compile within subdirectory, creating the executable within subdirectory 
  76.                     os.popen('g++ -g ' + cpp_name + ' -o ' + myprogram)
  77.                    #change back to higher directory 
  78.                     os.chdir('../')
  79.                 #make job description files (handy if they have a keyword that distinguishes them, like 'job')
  80.                     job_name='job_' + myprogram +'_s'+str(k+1)
  81.  
  82.                     f=open(job_name,'w')
  83.                     f.write('#PBS -N dynamics'+'_rep'+str(k+1)+'\n')
  84.                     f.write('#PBS -l walltime=100:00:00\n')
  85.                     f.write('#PBS -l ncpus=2\n')
  86.                     f.write('#PBS -j oe\n')
  87.                     f.write('cd '+ directory_name + '/' + dirName+ '\n')
  88.                     f.write('./' + myprogram + ' ' + str(k+1) + '\n')
  89.  
  90.                     f.close'
  91.                 ]
  92.             for i in l:
  93.                 c.write(i)
  94.             c.close
  95.  
  96.             #make submit.py file
  97.             s = open('/submit.py','w')
  98.             s.close()
  99.  
  100.             s = open('/compile.py','w')
  101.             # this information I'll put inside the file
  102.             j = ['
  103.                   import os
  104.  
  105.                   seeds=5
  106.                   beginrep=0
  107.  
  108.                   for k in range(beginrep,beginrep+seeds):
  109.                      job_name='deathPert_h500_df_0_pr_0.015_per_0.5'+'_s'+str(k+1)
  110.                      os.popen('qsub '+'job_'+job_name)
  111.  
  112.                   os.chdir('..')
  113.                  '
  114.                  ]
  115.             for i in j:
  116.                 s.write(i)
  117.             s.close
  118.  
  119.  
Aug 12 '10 #1
2 1669
bvdet
2,851 Expert Mod 2GB
Source code is ascii text in a disc file. Why not read the text from the file? As in:
Expand|Select|Wrap|Line Numbers
  1. fn = 'file_read_code.py'
  2.  
  3. f = open('code.txt', 'w')
  4. f.write(open(fn).read())
  5. f.close()
  6.  
Aug 12 '10 #2
dwblas
626 Expert 512MB
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
Expand|Select|Wrap|Line Numbers
  1.             #make submit.py file
  2.             s = open('/submit.py','w')
  3.             s.close()
  4.  
  5.             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.
Expand|Select|Wrap|Line Numbers
  1.                   os.chdir('..')
It is almost always better to use the absolute path, as chdir can cause debugging headaches. Also
Expand|Select|Wrap|Line Numbers
  1. f.write('cd '+ directory_name + '/' + dirName+ '\n')
  2. #
  3. # os.path.join solves the path joining problems.
  4. 'cd os.path.join(directory_name, dirName, some_other_name) \n'
Aug 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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
2
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...
7
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...
6
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
5
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.
8
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...
3
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...
7
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.
5
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.