473,806 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert python script to the text?

1 New Member
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 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:
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 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
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
3452
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
1663
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...
7
13013
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
6
9952
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
5
2578
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
1891
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
3
1683
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...
7
2986
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
1656
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...
1
1879
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
0
9719
marktang
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...
0
10371
jinu1996
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...
1
10373
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,...
0
10111
tracyyun
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...
1
7650
isladogs
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...
0
5546
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...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
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...

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.