I have a Pathfile.dat with File names within that file, I am trying to copy a backup of the files if one does not exists already
I am having an issue with the function " if files.is_file():"
Below are the Errors that i seem to be getting
/home/admin/Pycharm/backup/test1
not Found /home/admin/Pycharm/backup/test2
not Found cp: missing destination file operand after ‘/home/admin/Pycharm/backup/test1’
Try 'cp --help' for more information.
sh: 2: /home/admin/Pycharm/backup/: not found
sh: 3: _copy: not found
cp: missing destination file operand after ‘/home/admin/Pycharm/backup/test2’
Try 'cp --help' for more information.
sh: 2: /home/admin/Pycharm/backup/: not found
sh: 3: _copy: not found
import os
from pathlib import Path
import logging
filename = 'config.dat'
Configlist = []
def createlist():
with open(filename) as f:
for item in f:
Configlist.append(os.path.abspath(item))
def copy():
for list in Configlist:
print(list)
files = Path(list)
if files.is_file():
print("Found")
else:
print("not Found")
os.system("cp -R " + list + " /home/admin/Pycharm/backup/ " + list + "_copy ")
createlist()
copy()
The problem isn't perfectly clear, but there is for sure a problem in the way you do create the string with the cp command
if you do a print of the string used in os.system method, you can see something like this:
cp -R /yourpathfile/file /home/admin/Pycharm/backup/ /yourpathfile/file _copy
cp takes two arguments as input (source and destination) When you concat list the second time you are using the name with the path, but you need only the file name.
Also there is a space at the end of the string "/home/admin/Pycharm/backup/ " so if you concat with the file name it will give you an error
I've tried to adjust the cp part this way, ant this seems to work:
import os
from pathlib import Path
import logging
filename = 'config.dat'
Configlist = []
def createlist():
with open(filename) as f:
for item in f:
Configlist.append((os.path.abspath(item), item))
def copy():
for (list,name) in Configlist:
files = Path(list)
if files.is_file():
print("Found")
else:
print("not Found")
string = "cp -R " + str(list[:-1]) + " /home/admin/Pycharm/backup/"+name[:-1] + "_copy "
print(string)
os.system(string)
createlist()
copy()