A, python presentations created file

A, python codes

 code show as below:

 # Create a txt file named mytxtfile, and writes the file msg 
DEF text_create (name, msg): 
    desktop_path = " E: \\ \\ PyTest "   # storage path txt file newly created 
    full_path = desktop_path + name + ' .txt '   # can also create a word document .doc 
    File = Open (full_path, ' w ' ) 
    file.write (msg)    # msg world is following the Hello! 
    # File.close () 
 
text_create ( ' mytxtfile ' , ' Hello world! ' )
 # call the function to create a .txt file named mytxtfile, and to write it to Hello world!

Two, C # to call python

1, the search IronPython installation package

 2, python call

 Project -> Add -> New Folder, named PythonFiles, the Python script to copy in this folder

 Add two references, and select IronPython.dll Microsoft.Scripting.dll below the root directory packet IronPython

 test.py

 Are Third, explicitly call python success

So we're not going to call but directly by calling the python python method of papers

 code show as below:

        static  void Main ( String [] args) 
        { 
            // Non-ASCII Character '\ XE6' in File coupled with Coding = UTF-8 #
             // default file save path 
            String localPath = Path.Combine (Directory.GetCurrentDirectory (), " PythonFIles " , " test.py " ); // get the application's current working directory 
            ScriptRuntime pyRuntime = Python.CreateRuntime ();
             Dynamic obj = pyRuntime.UseFile (localPath); 

            Console.WriteLine (obj.text_create ( " mytxtfile " , "Hello World!-python"));
            Console.ReadKey();
        }

 python code is as follows:

# Coding = UTF-8 
# Create a txt file named mytxtfile, and writes the file msg 
DEF text_create (name, msg): 
    desktop_path = " E: \\ \\ PyTest "   # to store the newly created file txt path 
    full_path = desktop_path + name + ' .txt '   # can also create a word document .doc 
    File = Open (full_path, ' w ' ) 
    file.write (msg)    # msg world is following the Hello! 
    File.close ( )
     return msg 
 
# text_create ( 'mytxtfile', 'Hello world!') 
# call the function to create a .txt file named mytxtfile, and to write it to Hello world!

 

Guess you like

Origin www.cnblogs.com/fger/p/12571963.html