0

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()
Nikaido
  • 4,443
  • 5
  • 30
  • 47
David
  • 239
  • 2
  • 3
  • 12
  • Hi David, you question is confusing me. If you append to a list multiple file paths, how are you checking in the copy function one by one the backup itself? -->Path(list) is then wrong and you will not have only a file there. In addition also the os.system command is throwing error because is not proper done. To copy a file you can also use the shutil module. – Marco smdm Nov 02 '16 at 12:51
  • Hi Marco the copy function is where i am trying to resolve.. Thanks – David Nov 02 '16 at 12:54
  • Then also the cp -R is wrong....The meaning -R, -r, --recursive copy directories recursively :-) just check the man. – Marco smdm Nov 02 '16 at 12:56

1 Answers1

1

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()
Nikaido
  • 4,443
  • 5
  • 30
  • 47