473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to move files based on file-ending from dirs and subdirs tospecific dir?

hey,

i am trying to move files with a specific file-ending (.msf) to dir above their
current location. my code so far works as long as all the files are on the same
dir level.

but how can i make it work if there are multiple subdirs with files inside of
them on different dir levels?
my code so far is:
---
top = 'f:\\test\\mail'
import os, time, itertools

#walk and find all files
allfiles = []
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
allfiles.append(os.path.join(root, name))
#remove all non .msf files
index = []
for match in allfiles:
if match.endswith('.msf'):
index.append(match)

indexdest = []
indexdest = ['%s\\..\\..\\%s\\%s\\%s' % (x , time.strftime('%Y%m%d%H%M%S'),
os.path.basename(os.path.normpath(x+'\\..')), os.path.basename(x)) for x in ind
ex]

#\\.. to remove the ending and than basename to add it back on
indexdest = [os.path.normpath(i) for i in indexdest]
indexdest = [i.replace('Mail', 'backups-msf') for i in indexdest]

for a, b in itertools.izip(index, indexdest):
os.renames(a, b)
--

i want the .msf files to be in a dir on the same level as where the top string
points to (in this case f:\test\backups-msf).

thanks,
Evan

p.s. is there a more accurate word for a directory level or something on the
same "tier" of a directory?

Jan 2 '07 #1
2 1423
In <ma***************************************@python. org>, Evan Carmi
wrote:
top = 'f:\\test\\mail'

[…]

indexdest = []
Here you bind the name `indexdest` to an empty list but replace it in the
very next line with another list. So this line is unnecessary.
indexdest = ['%s\\..\\..\\%s\\%s\\%s' % (x , time.strftime('%Y%m%d%H%M%S'),
os.path.basename(os.path.normpath(x+'\\..')), os.path.basename(x)) for x in ind
ex]
This line is quite complicated and I don't really grasp what it's doing.
Maybe some comments, possible with examples, are needed here. And maybe
then you'll see why it doesn't work as wanted!? If these are the
destination paths and they should be at the same level as `top`, where is
`top` here? Shouldn't that be a prefix of all destination paths?

Ciao,
Marc 'BlackJack' Rintsch
Jan 2 '07 #2
Marc 'BlackJack' Rintsch <bj_666 <atgmx.netwrites:
This line is quite complicated and I don't really grasp what it's doing.
Maybe some comments, possible with examples, are needed here. And maybe
then you'll see why it doesn't work as wanted!? If these are the
destination paths and they should be at the same level as `top`, where is
`top` here? Shouldn't that be a prefix of all destination paths?

Ciao,
Marc 'BlackJack' Rintsch
I figured out the problem, with a lot of help.
Thanks for the help
Evan
Jan 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ...
1
by: CPNZ | last post by:
I have a database i am m oving to another server, during the process I am moving the data and log files to another drive.(Which I have done countless times before with no problems) The problem I...
1
by: Stein | last post by:
I've seen a couple postings on moving files to folders with set source and destination paths, but I still need a little help taking it a step further. I have a folder with 2,000 Word docs that I...
1
by: Mark | last post by:
Hi All, Sorry if this is a basic question but I can't get this to work. I have files randomly FTP'd to a directory and I need to import them. That I can do but what I can't do is move them once...
1
by: Danny | last post by:
Hello, I was hoping someone might be able to shed some light on this bizarre behaviour. I have observed some strange behaviour with System.IO.FIle.Move where it seems to be copying a file as...
2
by: Matthew Morvant | last post by:
I have am working with a text file and wish to move the file when I am done working with it. I have the following defined: InputFolder: "D:\dhc_work\indexed\" OutputFolder:...
1
by: Matthew Eno | last post by:
I have a problem where I'm developing some code that moves files from one place to another (on the same drive) with the destination file having a new name. What's happening is that the line of...
3
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot...
3
by: jaeden99 | last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with...
2
by: xavix | last post by:
Hello, How can I move files based on age in the following scenario: $SOURCE contains some subfolders with some files inside them. Each subfolder contain files of the same date, but the last...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.