Created
June 5, 2024 08:35
-
-
Save MzHmO/71a84428d73758c30fbed320e0b355c5 to your computer and use it in GitHub Desktop.
no os py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using IronPython.Hosting; | |
using Microsoft.Scripting.Hosting; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var engine = Python.CreateEngine(); | |
var pythonScript = @" | |
import clr | |
clr.AddReference('System') | |
from System import IO, DateTime | |
def list_files(directory): | |
try: | |
files = IO.Directory.GetFiles(directory) | |
print(f'Files in ""{directory}"":') | |
for file in files: | |
print(f' - {file}') | |
except IO.FileNotFoundException: | |
print(f'Directory ""{directory}"" not found.') | |
except IO.IOException as e: | |
print(f'Error accessing directory ""{directory}"": {e.Message}') | |
def create_log_file(): | |
log_dir = IO.Path.Combine(IO.Directory.GetCurrentDirectory(), 'logs') | |
if not IO.Directory.Exists(log_dir): | |
IO.Directory.CreateDirectory(log_dir) | |
log_file_path = IO.Path.Combine(log_dir, f'log_{DateTime.Now.ToString(""yyyyMMdd_HHmmss"")}.txt') | |
with IO.File.CreateText(log_file_path) as log_file: | |
log_file.WriteLine('Sample log entry') | |
log_file.WriteLine(f'Log created at {DateTime.Now.ToString()}') | |
print(f'Log file created at: {log_file_path}') | |
print('Running complex Python script') | |
list_files('.') | |
create_log_file() | |
"; | |
engine.Execute(pythonScript); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment