python文件嵌入问题

希望能将程序二嵌入到程序一中 麻烦了
程序一

import os
import re
import datetime
import numpy as np
import pandas as pd
if __name__ == '__main__':
    filenames_in = r'C:\Users\wxy\Desktop\111'  # 输入文件的文件地址
    filenames_out = r'C:\Users\wxy\Desktop\333'  # 新文件的地址
    pathDir = os.listdir(filenames_in)
    for allDir in pathDir:
        child = re.findall(r"(.+?).csv", allDir)  # 正则的方式读取文件名,去扩展名
        if len(child) >= 0:  # 去掉没用的系统文件
            newfile = ''
            needdate = child  #### 这个就是所要的文件名
        domain1 = os.path.abspath(filenames_in)  # 待处理文件位置
        info = os.path.join(domain1, allDir)  # 拼接出待处理文件名字
        # ------------数据处理过程---------------
        print(info, "开始处理")
        df = pd.DataFrame(pd.read_csv(info))
      #处理过程
        domain2 = os.path.abspath(filenames_out)  # 处理完文件保存地址
        outfo = os.path.join(domain2, allDir)  # 拼接出新文件名字
        df.to_csv(outfo, encoding='utf-8',index=False)
        print(info, "处理完")
endtime = datetime.datetime.now()
 
 

程序二

import networkx as nx
import itertools
import sys

in_file = r'C:\Users\wxy\Desktop\333\hs_300_2.csv'
print(in_file)
out_file = r'C:\Users\wxy\Desktop\re\777.csv'
print(out_file)
#node_file = r'C:\Users\wxy\Desktop\re\888.csv'
#print(node_file)
vl='0.75'
f = open(out_file, 'w')
#g = open(node_file, 'w')

G = nx.read_edgelist(in_file, comments='#', data=(('corr', float), ('metric', float)))
G.remove_edges_from(nx.selfloop_edges(G))

nodes = []
nodes = list(G.nodes)

mst = list(nx.algorithms.tree.mst.minimum_spanning_edges(G, algorithm='prim', weight='metric', data=True))

for x in mst:
    f.write(x[0] + '\t' + x[1] + '\t')
    for edge in G.edges(data=True):
        if x[0] == edge[0] and x[1] == edge[1]:
            wt = edge[2]['metric']
            f.write("%f\n" % (wt))
            break
        elif x[0] == edge[1] and x[1] == edge[0]:
            wt = edge[2]['metric']
            f.write("%f\n" % (wt))
            break

for edge in G.edges(data=True):
    flag = 0

for x in mst:
    if x[0] == edge[0] and x[1] == edge[1]:
        flag = 1
    elif x[0] == edge[1] and x[1] == edge[0]:
        flag = 1

if flag == 0 and edge[2]['corr'] >= float(vl):
    f.write(edge[0] + '\t' + edge[1] + '\t')
wt = edge[2]['metric']
f.write("%f\n" % (wt))

#for node in nodes:
#    g.write(node + '\n')

其中in_file对应filenames_in,out_file对应filenames_out 谢谢

1.假如第二段代码文件名为pdata.py,将其中代码封装成一个函数pro_data(),参数为infile,outfile。
2.在第一个代码中导入一下,from pdata import proc_data,在第一段代码中,遍历文件,调用函数pro_data即可对多个文件进行处理。

是不是分别设置文件路径就行了?如果是两段代码和在一起,将每个文件的代码定义为函数,通过调用就行了

  1. 把程序二封装成函数给程序一调用
  2. 程序二用argparse封装成接收参数的py文件,程序一通过os.system()调用

相比较而言肯定方法1简单