LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2022, 03:09 PM   #1
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Rep: Reputation: Disabled
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.
 
Old 08-23-2022, 04:12 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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.

Last edited by dugan; 08-23-2022 at 04:58 PM.
 
Old 08-23-2022, 05:22 PM   #3
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Original Poster
Rep: Reputation: Disabled
[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.
 
Old 08-24-2022, 09:39 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Maybe
Code:
 filePath = os.path.join(root, name)
 if os.path.islink(filePath) == True:
    continue
 
Old 09-26-2022, 12:03 PM   #5
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Original Poster
Rep: Reputation: Disabled
this worked

THIS WORKED !!
Code:
import os
import shutil
import pathlib
source1 = "/Volumes/TM3gbBu/doxx"
source2 = "/Volumes/crucialX8/fr800gb21inMac/fr6tb05jan22/
mainData-3copies/MainData4TM-ORIG"
dest1 = "/Volumes/TM3gbBu/destFylzMTsPy/"
dest2 = "/Volumes/crucialX8/destMTss0bytesPy-crux8/dest0byteFylzPy-crux8"
counter = 0
print(' ')
print('counter is ', counter)
for root, dirs, files in os.walk("."):
    print('btw4loops')
    for fileName in files:
        print('fileName is ', fileName)
        filePath = os.path.join(root, fileName)
        print('filePath is ', filePath)
        fileExt = pathlib.Path('fileName').suffix
        print('file extension is ', pathlib.Path('fileName').suffix)
        if fileExt != 'js' and os.path.isfile(filePath) and os.path.exists(filePath) and os.path.getsize(filePath) == 0:
            print('os.path.getsize(filePath) is ', os.path.getsize(filePath))
            counter += 1
            print('counter is ', counter)
            incrFN = "_" + str(counter) + "_" + fileName
            print('incrFN is ', incrFN)
            shutil.move(filePath, "/Volumes/crucialX8/destMTss0bytesPy-crux8/0bCrx/" + incrFN)
 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to fix java.io.IOException: Cannot run program "getContentHtml" (in directory "."): error=2, no such file exception?? xg3571 Programming 4 12-14-2021 02:35 AM
How to fix error: " error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory"? justa_skid Linux - Newbie 2 02-22-2021 02:23 AM
Rocket/Core/Python/Python.h: No such file or directory shogun1234 Linux - Games 3 10-12-2014 06:32 AM
[SOLVED] error: asm/ppc_asm.h: No such file or directory and error: asm/processor.h: No such f Thirupathip Linux - Newbie 3 01-25-2013 03:02 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration