Мой код дает это ...
Target_1 заменен на этот: # $
-wd /Users/tk/Desktop/sc/'ranked/024', 'ranked/023', 'ranked/015', 'ranked/012', 'ranked/041', 'ranked/013', 'ranked/014', 'ranked/022', 'ranked/025', 'ranked/040', 'ranked/038', 'ranked/007', 'ranked/009', 'ranked/036', 'ranked/031', 'ranked/030', 'ranked/008', 'ranked/037', 'ranked/001', 'ranked/039', 'ranked/006', 'ranked/042', 'ranked/045', 'ranked/020', 'ranked/027', 'ranked/018', 'ranked/011', 'ranked/016', 'ranked/029', 'ranked/044', 'ranked/043', 'ranked/017', 'ranked/028', 'ranked/010', 'ranked/026', 'ranked/019', 'ranked/021', 'ranked/003', 'ranked/004', 'ranked/032', 'ranked/035', 'ranked/034', 'ranked/033', 'ranked/005', 'ranked/002'
Цель_2 заменена на эту:
gerun '024', '023', '015', '012', '041', '013', '014', '022', '025', '040', '038', '007', '009', '036', '031', '030', '008', '037', '001', '039', '006', '042', '045', '020', '027', '018', '011', '016', '029', '044', '043', '017', '028', '010', '026', '019', '021', '003', '004', '032', '035', '034', '033', '005', '002'.out
Но я бы хотел иметь только один:
target_1: /ranked/024,
or #$ -wd /ranked/025,
or #$ -wd /ranked/028, or etc
(путь зависит от того, где будет расположен файл)
target_2: 024, 025, or 015, etc
(та же идея, что и у target_1)
Исходное содержимое файла для строк:
$ -wd /Users/tk/Desktop/sc/target_1
target_2.out
import os
import shutil
import sys
###########################################################
def get_directories(path):
directories = [x for x in os.listdir(path)] # 'directories' = x which is in the 'path'
directories = [(path + x) for x in directories] # now directories = x that x is x + path
return directories
###########################################################
root = '/Users/tk/Desktop/forPython/sc/'
path = '/Users/tonggihkang/Desktop/forPython/scott/top_structures/'
dest = '/Users/tk/Desktop/forPython/sc/ranked/'
# copy and paste the essential files to the same directories where .xyz located
_dir = get_directories(dest) # _dir = /Users/tonggihkang/Desktop/scott/ranked/023 ... Full paths
############################################################
# Produce "trash.sh" with right path for output files
############################################################
dir_1 = [(x.replace(root, '')) for x in _dir] # dir_1 = ranked/001 ranked/002... will be placed on the string, 'target_1'
dir_2 = [(x.replace(dest, '')) for x in _dir] # dir_1 = 001 002... will be placed on the string, 'target_2'
foo = 'trash_1.sh' # The file for the Reading
bar = 'trash.sh' # The file for the writing
#boo = 'target_1' # The first target string (which will replaced with dir_1)
#far = 'target_2' # The second target string (which will replaced with dir_2)
for i in range(len(_dir)):
with open(_dir[i] + '/' + foo, 'r') as f:
edit_1 = f.read().replace("target_1", str(dir_1).strip('[]'), 1)
edit_2 = edit_1.replace("target_2", str(dir_2).strip('[]'), 1)
with open(_dir[i] + '/' + bar, 'w') as f: # open new file (trash.sh) to copy trash_1.sh down
f.write(edit_2)
#fout.write(line.replace('target_1', str(dir_1).strip('[]')).replace('target_2', str(dir_2).strip('[]'))
os.remove(_dir[i] + '/'+ 'trash_1.sh')
print('"trash_1.sh has been removed and "trash_1.sh" has been produced with right output file path')