Move a file. WinError3. Help path navigation

Question:
In the interactive python console,

shutil.move(os.path.join(os.path.realpath('.'),'Downloads', 'Monthly Budget.xlsx'),os.path.join(os.getcwd(),'python_project','budget.xlsx')

gives error

Actual file:
Source : C:\Users\malli\Downloads
Destination: C:\Users\malli\OneDrive\Desktop

Getting an error - WinError3

1 Like

Hi @ashish9670 !
Could you try using $ mv path/to/old-folder/ path/to/new-folder/ instead?

ok. and file name of file ?

Oh… hadn’t thought of that…

@ashish9670 What is the name of the file that you want to move?

Nate, the file to move is named "Monthly budget.xlsx

Source folders has many file

move monthly budget.xlsx from C:\Users\malli\Downloads to C:\Users\malli\OneDrive\Desktop\python_project
cwd ‘C:\Users\malli\OneDrive\Desktop’

shutil.move(os.path.join(os.path.realpath('..'), 'Downloads','Monthly budget.xlsx'), os.path.join(os.getcwd(), 'python_project', 'budget.xlsx'))

giving error!

In the windows command prompt, perhaps try this, using absolute paths:

move "C:\Users\malli\Downloads\Monthly budget.xlsx" "C:\Users\malli\OneDrive\Desktop\python_project"

(Check the paths to make sure they are correct)

I have little experience with Windows so this is a very convoluted way, using absolute paths.

1 Like

I know. But I want a Python command.

Oh.
I am not really sure, but try doing some debugging, maybe:

source = os.path.join(os.path.realpath('.'), 'Downloads', 'Monthly Budget.xlsx')
dest = os.path.join(os.getcwd(), 'python_project', 'budget.xlsx')
print(f"{source = }")
print(f"{dest = }")
shutil.move(source, dest)

Check that these paths are the same as the file paths. Also look at the error message.

If that doesn’t help, then a platform-specific Windows command prompt thing might work:

os.system(
  'move "C:\Users\malli\Downloads\Monthly budget.xlsx" "C:\Users\malli\OneDrive\Desktop\python_project"'
)

Thank you :+1: Looks good, will try.

Error on entering shutil.move(source, dest):
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/shutil.py", line 862, in move
    os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/ashishrai/Downloads/Monthly Budget.xlsx' -> '/Users/ashishrai/Desktop/python_projects/budget.xlsx'

Hm, are you running this code on replit repl or directly on your computer?
When a repl is run, it is run on a replit server, not run on your computer.

(The file paths in the error message do not look like Windows file paths)

Also, try os.path.exists() on each part of the file paths to see what directory is missing.

Directly on computer - mac.

1 Like

Mac is Unix-based IIRC, so you can use the mv command.

1 Like

Try this code maybe, see what it says. It will tell what the missing path is.

source = os.path.join(os.path.realpath('.'), 'Downloads', 'Monthly Budget.xlsx')
dest = os.path.join(os.getcwd(), 'python_project', 'budget.xlsx')
print(f"SOURCE PATH: {source}")
print(f"DEST PATH: {dest}")
try_path = ''
print('testing source path...')
for path in source.split('/'):
  try_path += path
  if os.path.exists(try_path):
    print(f'Path <{try_path}> exists')
  else:
    print(f'Path <{try_path}> does NOT exist')
    break
else:
  assert os.path.exists(source)
  print('Source path exists')
  print('testing dest path...')
  try_path = ''
  for path in dest.split('/'):
    try_path += path
    if os.path.exists(try_path):
      print(f'Path <{try_path}> exists')
    else:
      print(f'Path <{try_path}> does NOT exist')
      break
  else:
    assert os.path.exists(dest)
    print('All paths exist. Moving source to dest...')
    shutil.move(source, dest)
2 Likes