0

I want to try using this shutil.copy and my files got some symbolic characters inside its name, so I need to use this shutil.copy(src, dst, *, follow_symlinks=True) command. but the compiler keeps giving me the invalid syntax messages. I had google and didn't find any solution yet for this. anyone can point out what is wrong with my syntax? because I already tried to print out the files inside those directories and it is okay, the network shared folder also got the permission and so on. but don't know what is wrong with my syntax. help me. thanks

This is my current script

enter image description here

and here is the output I got

File "C:\Users\1000266946\Desktop\sa\we.py", line 17

shutil.copy( files, parse_destination_path, * , follow_symlinks = True)
                                              ^

SyntaxError: invalid syntax [Finished in 0.3s]

Arvind Maurya
  • 910
  • 12
  • 25
  • Remove the `, *` in your call. – MSeifert Nov 07 '19 at 10:00
  • if i remove that, it will give me this – SectumSempra Nov 07 '19 at 10:04
  • Traceback (most recent call last): File "C:\Users\1000266946\Desktop\sa\we.py", line 17, in shutil.copy( files, parse_destination_path, follow_symlinks = True) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 248, in copy copyfile(src, dst, follow_symlinks=follow_symlinks) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 120, in copyfile with open(src, 'rb') as fsrc: – SectumSempra Nov 07 '19 at 10:05
  • FileNotFoundError: [Errno 2] No such file or directory: '2019-11-05.db' [Finished in 0.4s] – SectumSempra Nov 07 '19 at 10:05
  • oh ya sorry. btw, actually i just started learning python like 2 or 3 days ago hehe – SectumSempra Nov 07 '19 at 10:06
  • general comment: please paste the actual code, not an image. – FObersteiner Nov 07 '19 at 12:36

1 Answers1

0

first of all: the asterisk as you show it can be used in function definitions to define keyword-only arguments (see e.g. here) - but you don't use it in a function call. So in shutil.copy(src, dst, *, follow_symlinks=True) in the docs, it tells you that follow_symlinks can only be used as a keyword argument, not as a positional argument.

second: take a look at the return value of os.listdir(). it returns only file names, without the path as noted in the docs. shutil.copy() however expects a full path, not only the filename. so what you could do is

for file in parse_source_file_list:
    shutil.copy(os.path.join(parse_source_path, file), os.path.join(parse_destination_path, file))

further reading: you might also be interested in having a look at glob and pathlib for file handling purposes (docs here and here).

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • ohh like that ya, thanks for pointing out my mistakes and also thanks for the guidance u provided, now i know that i misunderstood how does it works before. those documentation really help me. later i will go thru the glob and pathlib also. :D – SectumSempra Nov 07 '19 at 15:31