简体   繁体   English

在python中的os.system子进程内合并两个URL

[英]Merge two URLs inside a os.system subrprocess in python

I am new to Python. 我是Python的新手。 I am trying to automate a patching activity. 我正在尝试自动化修补活动。

The expectation is to checkout a file from SVN and patch it. 期望是从SVN签出文件并对其进行修补。 First the user will enter a url and second input will be the filename from the repository. 首先,用户将输入URL,第二个输入将是存储库中的文件名。 I want to export the file to my localsystem. 我想将文件导出到我的localsystem。 I have a code like this, 我有这样的代码,

 import os
 urlpath =input('Enter the URL')
 Filename =input('Enter the filename with Extension')
 os.system("svn export 'urlpath'+'/'+'Filename'")

Input: https://svn.abcdf.xyz filename: utility.java 输入: https ://svn.abcdf.xyz filename:utility.java

The code should export the file from https://svn.abcdf.xyz/utility.java . 代码应从https://svn.abcdf.xyz/utility.java导出文件。

Is there anyother way I can do this? 有没有其他方法可以做到这一点? or how to combine two different variables that should be seprated by / inside os.system. 或者如何组合应该由/.sys.system内部分隔的两个不同变量。

As already indicated in comments, the argument to os.system is just a string; 正如在注释中已经指出的那样, os.system的参数只是一个字符串; so you'd use Python's regular string formatting features to create it. 所以你要使用Python的常规字符串格式化功能来创建它。

import os
urlpath = input('Enter the URL')
filename = input('Enter the filename with extension')
os.system("svn export '{0}/{1}'".format(urlpath, filename))

However, you probably should prefer subprocess.run as strongly hinted in the os.system documentation. 但是,你应该更喜欢subprocess.run的作为强烈暗示os.system文档。

import subprocess
urlpath = input('Enter the URL')
filename = input('Enter the filename with extension')
subprocess.run(['svn', 'export', urlpath+'/'+filename],
    check=True)

With check=True you get an explicit error if the command fails ( os.system will only let you know through the exit status, which you were not examining) and because we don't use shell=True , this should be a tiny bit faster. 使用check=True ,如果命令失败,你会得到一个明确的错误( os.system只会告诉你通过你没有检查的退出状态),因为我们不使用shell=True ,这应该是一点点快点。 Notice also how the first argument is a list of tokens, not a string which the shell has to pick apart again (remember, there is no shell here now). 还要注意第一个参数是一个标记列表,而不是一个shell必须再次分开的字符串(记住,现在这里没有shell)。

subprocess.run() was introduced in Python 3.5; subprocess.run()是在Python 3.5中引入的; if you need to support older versions, maybe explore replacing it with subprocess.check_call() 如果你需要支持旧版本,可以探索用subprocess.check_call()替换它

A much better design would get rid of the interactive input calls and allow the user to pass in parameters as command-line arguments ( sys.argv ); 一个更好的设计将摆脱交互式input调用,并允许用户作为命令行参数传递参数( sys.argv ); that way, you get to use the shell's history and file name completion features so you don't need to type file names in full by hand, and can easily recall files you have recently used. 这样,您就可以使用shell的历史记录和文件名完成功能,因此您无需手动完整地键入文件名,并且可以轻松调用最近使用过的文件。 This also makes it feasible to build additional automation around your script, because then programs can easily drive other programs. 这也使得围绕脚本构建额外的自动化成为可能,因为程序可以轻松地驱动其他程序。

import sys
import subprocess
import logging

if len(sys.argv) != 3:
    logging.error('syntax: {0} baseurl filename'.format(
        sys.argv[0].split('/')[-1}))
    sys.exit(2)

urlpath = sys.argv[1]
filename = sys.argv[2]
subprocess.run(['svn', 'export', urlpath+'/'+filename],
    check=True)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM