I want to use Python to run the cp command on Linux, thus copying a file. I have this code:
newfile = "namePart1" + dictionary[key] + "namePart2"
os.system("cp cfn5e10_1.lp newfile")
How can I make it so that the text newfile on the second line is replaced with the newfile variable that I calculated on the previous line?
Use
shutil.copyfileto copy a file instead ofos.sytem, it doesn’t need to create a new process and it will automatically handle filenames with unusual characters in them, e.g. spaces —os.systemjust passes the command to the shell, and the shell might break up filenames that have spaces in them, among other possible issues.For example: