473,657 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(r oot, name))
#remove all non .msf files
index = []
for match in allfiles:
if match.endswith( '.msf'):
index.append(ma tch)

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

#\\.. to remove the ending and than basename to add it back on
indexdest = [os.path.normpat h(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 1434
In <ma************ *************** ************@py thon.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.basenam e(os.path.normp ath(x+'\\..')), os.path.basenam e(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.netwrite s:
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
7759
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 ---------------------------------------------------------------------------- ----------------- import re, os, time, shutil os.chdir(os.environ+os.environ+"\\Desktop") DESKTOP = os.listdir(os.environ+os.environ+"\\Desktop")
1
4411
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 am having is after I restore the database using the following statement : RESTORE DATABASE CMAMSPROD FROM DISK = ’C:\Databasename.BAK’ WITH STANDBY = ’D:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Databasename\databasename.STANDBY’ ,MOVE...
1
2404
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 generated using Word automation. Since the file names were generated from a table in Access, I would then like to use Access to sort those files into various folder based on names stored within the table. For example: There is a Word doc...
1
4263
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 they have been imported (Currently I am using the Kill command). From the help file, I found the MoveFile method but I don't get the "FileSystemObject". Syntax is: object.MoveFile source, destination object Required. Always the name of a...
1
3821
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 opposed to actually moving it. The following is a description of my setup: Two separate machines (A and B) both running Windows Server 2003. Machines
2
1813
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: "D:\dhc_work\classified\" FileName: "pinkf065111-26092005632633405685338094~LWS1A_20050209_105314.pdf.txt" InputFile: InputFolder + FileName OutputFile: OutputFolder + FileName
1
3048
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 code where I do the file.move is taking a very long time (in the order of around 7 seconds for a simple 30kb file). The only thing that I can think may be causing the problem is that theere is around 1.1 million files to move, so perhaps it's the
3
3275
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 (i.e. the root directory). This is the code: <script runat="server"> Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs) File.Move(fudFileSource.FileName, txtFileDest.Text) 'File.Move("F:\4.jpg", "C:\4.jpg") End Sub
3
5449
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 users if it's ok to delete. Thanks in advance. i found one that was really close but it only looks at one folder. I need it to look at files within folders and subfolders too. Dim oFSO, wshShell, FileCol, oFolder, objTextFile, shareLength,...
2
11538
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 modofication date of the subfolder can be more recent than the date of the files inside it, e.g.: $tree . -D .
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.