简体   繁体   English

如何在不同目录的不同计算机上找到同一文件夹?

[英]how to locate a same folder on different machines on different directory?

I have two folders named Student and Faculty in the directory /home/ubuntu/Desktop/Pythontraining . 我在目录/home/ubuntu/Desktop/Pythontraining有两个名为StudentFaculty文件夹。 I need to save totally 10 files inside Student folder and 3 files inside the Faculty folder.I need to do the same in another system where the Student and Faculty folders are present in different directory(say: /home/documents/college ).How to store the file into the respective folders on two different machines without hardcoding the path? 我需要在Student文件夹中总共保存10个文件,在Faculty文件夹中总共保存3个文件。我需要在另一个系统中执行相同的操作,其中StudentFaculty文件夹位于不同的目录中(例如/home/documents/college )。将文件存储到两台不同计算机上的相应文件夹中,而无需对路径进行硬编码?

For this kind of problem, you have multiple solutions : 对于此类问题,您有多种解决方案:

Create environment variable on each machine, and inside the script do stuff like this : 在每台计算机上创建环境变量,然后在脚本内部执行以下操作:

import os
student_path = os.environ['STUDENT_PATH']
faculty_path = os.environ['FACULTY_PATH']

print(student_path, faculty_path)

Personal opinion : I don't like to configure my scripts using environment variables as the ones you chose may be used by another software + it is always messy to debug 个人意见:我不喜欢使用环境变量来配置脚本,因为您选择的脚本可能会被其他软件使用+调试总是很麻烦


Use arguments 使用参数

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--student")
parser.add_argument("-f", "--faculty")

args = parser.parse_args()
student_path = args.student
faculty_path = args.faculty

print(student_path, faculty_path)

And then call your script like this and adapt this line depending on the machine 然后像这样调用您的脚本并根据计算机调整此行

python <yourscript> -s <student_path> -f <faculty_path>

Personal opinion : I use arguments when I want to control a small amount of parameters on my scripts to change its behavior (verbose, nb of cpus, ...). 个人观点:当我想控制脚本中的少量参数以更改其行为(冗长,cpus的nb等)时,会使用参数。


Create a config file and use configparser 创建一个配置文件并使用configparser

config.ini file config.ini文件

[Paths]
student_path=<path_on_machine>
faculty_path=<path_on_machine>

Usage on script : 脚本用法:

import configparser

config = configparser.ConfigParser()
config.read('config.ini')
student_path = config.get('Paths', 'student_path')
faculty_path = config.get('Paths', 'faculty_path')

print(student_path, faculty_path)

And then deploy different config.ini files on each machine (tools like ansible can help you to automate this) 然后在每台机器上部署不同的config.ini文件( ansible之类的工具可以帮助您自动化)

Personal opinion : I use config files when I need to adapt parameters when deploying on new machines. 个人意见:在新机器上部署时,当我需要调整参数时,会使用配置文件。 I don't like to use arguments for this purpose as I don't want to specify the same values every time I use my script (often these kind of parameters don't have good default values). 我不想为此使用参数,因为我不想每次使用脚本时都指定相同的值(通常,这类参数没有很好的默认值)。


Create a module 创建一个模块

You can also create a module to store these parameters instead of a config file. 您也可以创建一个模块来存储这些参数,而不是配置文件。

my_config.py my_config.py

student_path="<path_on_machine>"
faculty_path="<path_on_machine>"

And then import it 然后导入

script.py script.py

import my_config

print(my_config.student_path, my_config.faculty_path)

I don't have any personal opinion on config files vs config modules. 我对配置文件和配置模块没有任何个人看法。 Read this if you want some elements of comparison. 如果您需要比较的某些元素,请阅读文章。

You can use walk library for finding destination folder path. 您可以使用步行库来查找目标文件夹路径。 It's working best if you have only one folder for each search name: 如果每个搜索名称只有一个文件夹,则效果最佳:

import os

start = "/home/"

for dirpath, dirnames, filenames in os.walk(start):
    found = False
    for dirname in dirnames:
        if dirname == 'Student':
            full_path = os.path.join(dirpath, dirname)
            found = True
            break
    if found:
        break

Output: 输出:

/home/.../Student /home/.../学生

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

相关问题 如何从不同目录,相同级别但不同文件夹中导入python模块 - How to import python module from different directory, same level, but different folder 如何在两台不同的机器上的同一个Django项目上工作? - How to work on the same django project on two different machines? 在不同的机器上自动选择正确的用户文件夹 - automatically select correct user folder on different machines 相同的Python代码,相同的数据,不同机器上的结果不同 - Same Python code, same data, different results on different machines 相同的PyTorch脚本未在其他计算机上运行 - Same PyTorch script not running on different machines 从同一父目录中的不同文件夹导入文件 - Importing files from different folder in the same parent directory 从存储在同一目录中不同文件夹中的.py文件导入function - Import function from a .py file stored in a different folder in the same directory 从同一父目录中的不同文件夹导入模块时出错 - Error when importing module from a different folder in the same parent directory 如何解压缩相同子目录但不同文件夹中的文件 - How to unzip files in the same subdirectories but a different folder 如何在 Python 的不同机器上找到相同的文件夹? - How to find the same folder on different machine in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM