IronPython 的核心 Python 模块的 C# 或其他 .NET 等效项?

标签 c# .net python ironpython

我想编译和分发(在 .net 上)一些与 IronPython 配合良好的 python 程序,但我是 .net 的新手,并且遇到与特定 python 模块相关的错误。有一个用于编译为低级 .net 的实用程序,它运行良好,但在处理公共(public)库时出现错误;在解释器中运行的代码不一定可以编译。例如,下面的代码利用了基本模块 shutilgetpassosgetpass.getuser() 以字符串形式返回用户名。 shutil 除其他外,还提供了一个复制函数(尽管我可以用纯 python 重写该函数并进行编译),并且 os 在这里用于获取文件夹信息,制作目录并取消链接文件。我如何按照以下方式全部或部分调整内容,以仅使用 .net 原生的库?如果有人使用 IronPython 作为从 python 到学习 .net 的桥梁,任何相关的提示都会受到赞赏。

import shutil
import os
import getpass

uname = getpass.getuser()

folders = ["/users/"+uname+"/location", "/users/"+uname+"/other_location"]

for folder in folders:
    for root, dir, files in os.walk(folder):
        for file in files:
            file_name = os.path.join(root, file)
            time_stamp = os.stat(file_name).st_mtime
            time_dict[time_stamp] = file_name
            working_list.append(time_stamp)


def sync_up():
    for item in working_list:
        if item not in update_list:
            os.remove(item)
        else:
            shutil.copy2(item, some_other_folder)

def cp_function(target=some_folder):
    if os.path.exists(target):
        sync_up()
    else:
        try:
            os.mkdir(target)
            sync_up()
        except:
            print """error connecting
            """

最佳答案

.NET 中的 os (和 shutil)替代品位于 System.IO namespace 中。 .

The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

对于大多数文件文件操作,请尝试 System.IO.File class 的方法。 目录信息可通过 System.IO.Directory class 获取.

我不知道本地 os.walk 替代方案,请尝试使用 GetDirectoriesGetFiles 方法构建您自己的目录遍历器。 Directory.GetDirectories(String) doc 中有一个示例 RecursiveFileProcessor .

检索当前登录者的用户名的简单方法可能是 System.Environment.UserName属性。

一个简单的交互式 IronPython 示例:

>>> import clr
>>> from System import Environment
>>> Environment.UserName
'gimel'
>>> from System import IO
>>> IO.Directory.GetCreationTimeUtc('c:/')
<System.DateTime object at 0x000000000000002B [02/07/2006 12:53:25]>
>>> IO.Directory.GetLastWriteTimeUtc('c:/')
<System.DateTime object at 0x000000000000002C [09/11/2009 08:15:32]>
>>> IO.Directory.GetDirectories('C:/').Count
24
>>> help(IO.File.Copy)
Help on built-in function Copy:

Copy(...)
    Copy(str sourceFileName, str destFileName, bool overwrite)

        Copies an existing file to a new file.
         Overwriting a file of the same name is allowed.
...

关于IronPython 的核心 Python 模块的 C# 或其他 .NET 等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1699856/

相关文章:

c# - 不使用自定义成员资格提供程序的 ASP.NET MVC 登录 Controller 方法?

c# - 在不安全的 c# 中使用 Ws2_32.dll 是否比使用 .net 套接字更快?

c# - .NET core 3 : Order of serialization for JsonPropertyName (System. 文本.Json.序列化)

.net - 未将对象引用设置为对象的实例 - 解释?

python - Jinja2 - 检查一个查询集元素是否在另一个查询集中

python - 我怎样才能在 Tkinter 中获得带有滚动条的框架?

c# - 如何在字符串中查找所有匹配项

.net - 检索 Post 参数和值 MVC3

.net - 如何在 .NET 中将字符串转换为字节数组?

Python - 报告实验室 : Why do SPAN for more than two rows at the second page returns error?