0

I am new to Python. I am trying to automate a patching activity.

The expectation is to checkout a file from SVN and patch it. First the user will enter a url and second input will be the filename from the repository. I want to export the file to my 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

The code should export the file from 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.

  • 1
    Use string formatting: `os.system("svn export %s/%s" % (urlpath, Filename))` – Chris Mar 28 '19 at 08:46
  • Possible duplicate of [How to join components of a path when you are constructing a URL in Python](https://stackoverflow.com/questions/1793261/how-to-join-components-of-a-path-when-you-are-constructing-a-url-in-python) – Georgy Mar 28 '19 at 09:31

1 Answers1

0

As already indicated in comments, the argument to os.system is just a string; so you'd use Python's regular string formatting features to create it.

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.

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. 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).

subprocess.run() was introduced in Python 3.5; if you need to support older versions, maybe explore replacing it with 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); 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. 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)
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I lowercased `filename`; proper case is conventionally reserved for class names in Python. – tripleee Mar 28 '19 at 09:54
  • Perhaps see also https://stackoverflow.com/a/51950538/874188 for (much) more on the fascinating topic of subprocesses in Python. – tripleee Mar 28 '19 at 09:55
  • Triplee, Thanks for the explanation. I tried running like this and does not seem to run properly, from sys import argv import os import subprocess script,url=argv #urlpath =input('Enter the URL') #filename =input('Enter the filename with extension') subprocess.run(['svn', 'export', 'url'],check=True) What am I missing here? Thanks again! – Parthasarathy J Mar 28 '19 at 11:16
  • You are still passing the literal string `'url'`. The arguments are in `argv[1:]`; the zeroth argument contains the name of the script itself. Really understand the difference between the symbol (variable or function name) `url` and the literal sequence of three characters `'url'`. – tripleee Mar 28 '19 at 11:20
  • Maybe read https://stackoverflow.com/q/16116446/874188 if you have difficulty distinguishing between strings and variables, though really you should perhaps finish the basic tutorial before attempting to write programs of your own. – tripleee Mar 28 '19 at 11:31