[SOLVED] How to Move File to Other Folder in Python. "No such file or directory" error
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to Move File to Other Folder in Python. "No such file or directory" error
Code:
for root, dirs, files in os.walk("/Volumes/TM3gbBu/miscx4now/fr6tb24jul22/miscxDox-ddupt-8gb/sort/BUs/doxx"):
for name in files:
fileSize = os.path.getsize(os.path.join(root,name))
if True:
filename = name
counter += 1
incrFN = str(counter) + '_' + filename
shutil.move(os.path.join(root, incrFN), "/Volumes/TM3gbBu/dest0bytePy/incrFN")
Above (abbreviated excerpt) script had "print" statements for various variables -- all correct.
But I can't get the "shutil.move" right.
I've tried quotes, different ways of specifying path . .
All I wanna do is append an incrementing number to the beginning of the filename (which appears to be working), and then move it to a different folder. It says . .
Code:
No such file or directory
referring to a file that it already acknowledged exists.
What am I doing wrong? Thank you much in advance.
Last edited by ericlindellnyc; 08-23-2022 at 03:17 PM.
Here's an untested attempt to fix the more obvious problems I eyeballed:
Code:
for root, dirs, files in os.walk("/Volumes/TM3gbBu/miscx4now/fr6tb24jul22/miscxDox-ddupt-8gb/sort/BUs/doxx"):
for name in files:
filePath = os.path.join(root, name)
if os.path.getsize(filePath) == 0:
counter += 1
incrFN = name + '_' + str(counter)
shutil.move(filePath, "/Volumes/TM3gbBu/dest0bytePy/" + incrFN)
In the future, consider stepping through it with pdb.
[QUOTE=dugan;6375699]Here's an untested attempt to fix the more obvious problems I eyeballed:
Code:
for root, dirs, files in os.walk("/Volumes/TM3gbBu/miscx4now/fr6tb24jul22/miscxDox-ddupt-8gb/sort/BUs/doxx"):
for name in files:
filePath = os.path.join(root, name)
if os.path.getsize(filePath) == 0:
counter += 1
incrFN = name + '_' + str(counter)
shutil.move(filePath, "/Volumes/TM3gbBu/dest0bytePy/" + incrFN)
This helped a lot.
Now, it seems the python loop is gagging on aliases (with broken links).
Can a test be applied to determine whether a file is an alias before python attempts to link to the target?
Then the file could be skipped in above loop.
Also, python is gagging on colornote backup files that look roughly like this . .
999999999999auto.dat.
I'm thinking it contains non-utf-8 characters.
Thank you again.
Last edited by ericlindellnyc; 08-23-2022 at 07:11 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.