How to convert python script to the text?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Ivanov
    New Member
    • Aug 2010
    • 1

    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:

    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Source code is ascii text in a disc file. Why not read the text from the file? As in:
    Code:
    fn = 'file_read_code.py'
    
    f = open('code.txt', 'w')
    f.write(open(fn).read())
    f.close()
    Last edited by bvdet; Aug 12 '10, 04:21 PM.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      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
      Code:
                  #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.
      Code:
                        os.chdir('..')
      It is almost always better to use the absolute path, as chdir can cause debugging headaches. Also
      Code:
      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'

      Comment

      Working...