#πŸ”’ Trying to copy a file to a folder, but error says file doesn't exist

14 messages Β· Page 1 of 1 (latest)

lunar matrix
#
print(local_path+"/src/reg_gen.py") #returns C:\Users\user\Desktop\prog\Repositories/program/src/reg_gen.py
print(path+"/registry")             #returns C:/Program Files/program/registry 
try:
    subprocess.call(f'copy {local_path+"/src/reg_gen.py"} {path+"/registry"}')
except subprocess.CalledProcessError as e:
    print("Error: " + str(e)) #returns FileNotFoundError: [WinError 2] The file cannot be found

Traceback says:

Traceback (most recent call last):
  File "c:\Users\user\Desktop\prog\Repositories\program\install.py", line 41, in <module>
    subprocess.call(f'copy {local_path+"/src/reg_gen.py"} {path+"/registry"}')
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 345, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
sharp parrotBOT
#

@lunar matrix

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lunar matrix
#

Both of the files exist, at least according to my browser and folder explorer since they open the file/folder

#

I'm not sure why this happens

brave meteor
#

oh no no no don't run a subprocess to copy files, use the shutil module

pulsar spindle
#

yeah, I'm not sure why you're using a CLI command just to copy a file.

#

(the reason why it's failing, I believe, is because subprocess.call takes arguments separately, not as one string - the way you wrote, it tries to call the executable 'copy {local_path+"/src/reg_gen.py"} {path+"/registry"}' with no arguments)
(oh, and also I think copy might be a builtin cmd command and not an executable at all, which'd require shell=True)

brave meteor
#

maybe it's a part of larger program, we don't know that

pulsar spindle
#

(Also, use pathlib, it's easy to mess up while manually concatenating strings representing paths.)

verbal carbon
sharp parrotBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.