简体   繁体   English

我如何在较低的目录中运行另一个 Python 文件

[英]How do i run another Python file in a lower directory

So I'm creating a program that reads itself, creates itself in every lower folder currently available and then runs each of those, in turn, copying itself lower and lower until it has all available directories below it with this program in it所以我正在创建一个程序,它可以读取自身,在当前可用的每个较低文件夹中创建自己,然后运行每个文件夹,依次将自身复制得越来越低,直到它下面的所有可用目录都包含该程序

 from glob import glob
 import os
 paths = glob('*/')
 file = open("Egg.py","r")
 code = ""
 for i in file:
     code = code + i
 file.close()
 for i in paths:
     name = "Egg.py"
     name = i + name
     file = open(name,"w+")
     file.write(code)
     file.close()
     #This is where i want to execute
     # Which has the name of the file location
     # followed by Egg.py
 print("Done")

this is the code I have so far, the file is called 'Egg.py' and creates the same script in the folders beneath it no problem.这是我到目前为止的代码,该文件名为“Egg.py”并在其下方的文件夹中创建相同的脚本没问题。 The issue I have is I cant then run these files as I create them to put the code in the files beneath them.我遇到的问题是我无法在创建这些文件时运行这些文件以将代码放在它们下面的文件中。 Does anybody know a quick and easy way to do this?有谁知道一种快速简便的方法来做到这一点? I would prefer if it was less code than more but any help at all is appreciated thanks.我更喜欢代码少而不是多,但任何帮助都表示感谢,谢谢。 for reference I have the code file next to another file like this: Folder Egg.py and in the folder I have 3 other folders FolderA FolderB FolderC etc thanks again作为参考,我在另一个文件旁边有一个代码文件,如下所示:Folder Egg.py,在该文件夹中我还有 3 个其他文件夹 FolderA FolderB FolderC 等再次感谢

If I understand correctly, there is no need for recursion here.如果我理解正确,这里不需要递归。 You can walk down the current path using the following method:您可以使用以下方法沿着当前路径走下去:

import os
walk = os.walk('.')

Then, you iterate over this walk, copying your file in every directory:然后,您遍历此步,在每个目录中复制您的文件:

# Read the contents of your current script
with open("Egg.py","r") as file:
    code = file.read()

# Explore your folders & subfolders iteratively
for root, foldernames, filenames in walk:
    with open(os.path.join(root, "Egg.py"), "w") as file:
        file.write(code)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在与另一个python文件不同的目录中运行python文件? - How do I run a python file in a different directory from another python file? 如何在 python 中运行另一个文件? - How do I run another file in python? 对于Python 3,如何从另一个目录导入.py文件? - For Python 3, how do I import a .py file from another directory? 如何在另一个Python文件中运行不在目录中的Python文件? - How to run a Python file not in directory from another Python file? 如何在另一个文件目录中的另一个脚本中导入Python文件? - How do I import a Python file within another script which is in another file directory? 如何使用python链接到另一个目录? - How do I link to another directory with python? 如何导入另一个目录中的文件? - How do I import a file that is in another directory? 如何在Python中运行bash脚本,但就好像它从另一个目录运行? - How do I run a bash script inside Python, but act as if it's running from another directory? 如何在pylab中运行一个python文件,该文件会执行另一个python文件? - How do I run a python file in pylab, which executes another python file? 如何使用 kivy 中的屏幕管理器在另一个 python 文件中运行 python 文件 - How do I run a python file inside another python file with Screen manager in kivy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM