Python脚本从文件输入文件中读取并写入文件

时间:2016-02-26 16:08:10

标签: python

您好,我需要在python中使用这个Javacode

我已用Java编程,但我需要在python中使用此代码

该脚本是从文件中读取和再次写入文件中键入的。

输入文件

  • cc_oo_g_csss.sh
  • cc_oo_guv_zppp.sh
  • cc_aba_ddd.sh
  • cc_aba_ccxyp.sh
  • cc_abus_pl_fa_part1.sh
  • cc_abus_pl_fa_part2.sh
  • cc_abus_pl_fa_part3.sh
  • cc_abus_pl_fa_part4.sh
  • c_abus_pl_fa_part5.sh
  • cc_abus_pl_fa_part6.sh
  • cc_abus_pl_fa_part7.sh
  • cc_abus_pl_fa_part8.sh
  • cc_abus_pl_fa_merge.sh
  • cc_abac_nsv_ssd.sh
  • cc_abac_kriv.sh
  • cc_rufrep_nia_inst_leg.sh
  • cc_rufrep_nia_inst_comb.sh
  • cc_rufrep_nia_inst_flow.sh
  • cc_rufrep_nia_inst.sh
  • cc_vision_kriv.sh
  • cc_vision_interface_part1.sh
  • cc_vision_interface_part2.sh
  • cc_vision_interface_part3.sh
  • cc_vision_interface_part4.sh
  • cc_vision_interface_merge.sh
  • cc_vision_deriv.sh
  • cc_ria_flows_rep_plain.sh
  • cc_iaed_fls_rep_merge.sh

我需要作为文件

cc_oo_g_csss.sh

cc_oo_guv_zppp.sh

cc_aba_ddd.sh

cc_aba_ccxyp.sh

D:\ Temp \ c_run_multiple_shell_skripts.sh

-S" cc_abus_pl_fauz_part1.sh,cc_abus_pl_fa_part2.sh,cc_abus_pl_fa_part3.sh,cc_abus_pl_fa_part4.sh, cc_abus_pl_fa_part5.sh,cc_abus_pl_fa_part6.sh,cc_abus_pl_fa_part7.sh,cc_abus_pl_fa_part8.sh"

-F cc_abus_pl_fa_merge.sh

cc_abac_nsv_ssd.sh

cc_abac_kriv.sh

cc_rufrep_nia_inst_leg.sh

cc_rufrep_nia_inst_comb.sh

cc_rufrep_nia_inst_flow.sh

cc_rufrep_nia_inst.sh

cc_vision_kriv.sh

D:\ Temp \ c_run_multiple_shell_skripts.sh

-S" cc_vision_interface_part1.sh,cc_vision_interface_part2.sh,cc_vision_interface_part3.sh, cc_vision_interface_part4.sh"

-F cc_vision_interface_merge.sh

cc_vision_deriv.sh

cc_ria_flows_rep_plain.sh

cc_iaed_fls_rep_merge.sh

public class shellsort {

public static Vector<String> sortforshell(String path) throws IOException{


    String[] input = readFile(path,Charset.defaultCharset()).split(System.getProperty("line.separator"));
    Vector<String> output = new Vector<String>();
    int i = 0;
    while(i<input.length){
        if(input[i].contains("part")){
            output.add("D:/hhh/cc_multiple_script.sh");
            String partLine = "-S ";
            partLine = partLine.concat(input[i]);
            String validate =input[i].substring(0,input[i].indexOf("part"));
            i++;
            while(input[i].contains("part")&&input[i].contains(validate)){
                partLine = partLine.concat(", "+input[i]);
                i++;
            }
            output.add(partLine);
            if(input[i].contains("merge")&&input[i].contains(validate)){
                output.add("-F "+input[i]);
                i++;
            }
        }else{
            output.add(input[i]);
            i++;
        }
    }


    return output;


}



static String readFile(String path, Charset encoding)  throws IOException {

          byte[] encoded = Files.readAllBytes(Paths.get(path));
          return new String(encoded, encoding);

        }



public static void main(String[]args) throws IOException{

    Vector<String> output = sortforshell("/input.txt");
    for(int i=0;i<output.size();i++){
        System.out.println(output.get(i));



    }



}

}

sjadjhdahs&gt; asdasda

1 个答案:

答案 0 :(得分:0)

读取文件:

f = open('/tmp/file')
out = f.read()
f.close()

写文件:

f = open('/tmp/file','w')
f.write('some text')
f.close()

简单的恩,嗯?