我可以在一个脚本中打开一个文本文件,然后在子脚本中写入该文件吗?

发布于 2024-12-05 03:22:56 字数 735 浏览 0 评论 0原文

这是一个有点奇怪的问题,但我有一堆自动化脚本(使用 Sikuli/Jython),我想从父脚本运行它们以用于回归目的。现在我还有一种捕获异常的方法,我想将其记录到文本文件中。这是它目前的工作原理:

Master Script --> specific test script
              --> specific test script
              --> etc (how ever many scripts there are)

我正在和我的一个朋友交谈,他建议每个脚本处理自己的错误,并且父脚本只打开一个文件,使文件位置成为全局变量,而子脚本只是附加到该文件。

基本上,我需要在 python 中做一些特殊的事情才能实现这一点吗?或者主脚本基本上只是“触及”其他脚本可以使用的文件。

这是“父脚本”的示例(其中没有任何代码来处理错误,也不打开文件,只是执行脚本)

for dirpath, dirnames, filenames in os.walk("c:\directory\to\the\scripts"):
    for filename in [f for f in filenames if f.endswith(".py")]:
        directory.append(os.path.join(dirpath, filename))
for entry in directory:
    execfile(entry)

This is a little bit of a strange question, but I have a bunch of automation scripts (using Sikuli/Jython), that i want to run from a parent script for regression purposes. Now I also have a method of capturing exceptions that I want to log to a text file. SO here's how it currently works:

Master Script --> specific test script
              --> specific test script
              --> etc (how ever many scripts there are)

I was talking to a friend of mine, and he recommended that each script handles its own errors, and that the parent script just opens a file, makes the file location a global variable, and the child scripts just append to that file.

Basically, do I need to do anything special in python to make this possible? or is the master script basically just "touching" a file that other scripts can use.

Here is an example of the "parent script" (this does not have any code to handle errors in it, nor does it open a file, just executes the scripts)

for dirpath, dirnames, filenames in os.walk("c:\directory\to\the\scripts"):
    for filename in [f for f in filenames if f.endswith(".py")]:
        directory.append(os.path.join(dirpath, filename))
for entry in directory:
    execfile(entry)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2024-12-12 03:22:56

现在我还有一种捕获异常的方法,我想将其记录到文本文件中。

使用日志记录http://docs.python.org/library/logging.html

问题已经已经被他们解决了。没有工作适合你。

Now I also have a method of capturing exceptions that I want to log to a text file.

Use logging. http://docs.python.org/library/logging.html

The problem has already been solved by them. No work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文