3

I know there have been some posts on how to move a file in python but I am a little confused. I am working on a program that has a file called test.txt

The file path is this: C:\Users\user\Desktop\Project1\Project1
I want to move it to: C:\Users\user\Documents\ProjectMoved
I tried different variations of what I have below

src="C:\\Users\\user\\Desktop\\Project1\\Project1\\test.txt"
dst="C:\\Users\\user\\Documents\\ProjectMoved"
shutil.move(src, dst)

I keep getting the error no such file in directory.

I was wondering if someone could help me out with the correct way to move the file.

4
  • Does C:\Users\user\Documents\ProjectMoved exist? You probably have to create it. Commented Jul 19, 2016 at 19:58
  • dst is not quoted correctly - too many leading "s. Commented Jul 19, 2016 at 19:58
  • @Rawing yes it does exist Commented Jul 19, 2016 at 20:00
  • are you trying to move the file test.txt to a directory or file called ProjectMoved? Commented Jul 19, 2016 at 20:04

2 Answers 2

5

Might be worth checking the file exists and then trying to specify paths using os.path.join:

import shutil
import os
from os.path import join

src = join('/', 'Users', 'username', 'Desktop', 'a.pdf')
dst = join('/', 'Users', 'username', 'Documents', 'a.pdf')

shutil.move(src, dst)

You can first verify if the src actually exists:

os.path.exists(src)
>>> True
Sign up to request clarification or add additional context in comments.

8 Comments

It does exists when I do it with the src=join
thank you it worked. Can you explain to me what join does? and why we needed the '/'
Also is there a way to search your whole computer for a specific file in python
os.path.join can create a full pathname from multiple components (among other things). You might want to use it cause then you can specify path names across any type of operating system. The 'slash' is the root directory (yes, it's an actual directory name). en.wikipedia.org/wiki/Root_directory
If you're starting in Python, be aware that the join function was imported from the os.path module. So you could remove the line from os.path import join and then use os.path.join instead of join in the above script, but you'd be best to read about importing modules in Python elsewhere (it's really a separate question). Searching for files using Python is a separate question - search SO for answers to this.
|
0

This problem can also be solved this way if your trying to move multiple .txt files from Folder A to folder B. This principle can be applied to your problem since your moving .txt file.

import os
import shutil
os.chdir('C:\\') 

dir_src = ("C:\\Folder A\\")
dir_dst = ("C:\\Folder B\\")

for filename in os.listdir(dir_src):
    if filename.endswith('.txt'):
        shutil.move( dir_src + filename, dir_dst)
    print(filename)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.