Moving a subfolder to a different parentfolder in python - python

Hi After 2 hours of googling and searching I am failing to find, or build a simple piece of code for the following setup:
3 folders, 2 files:
/home/folderA/text2.txt
/home/folderB/
/home/folderB/folder1
/home/folderB/text1.txt
Moving the "Unknown" content of folderB to folderA.
All the attempts at solving this issue are either deemed too trivial and redirected to "look up shutil." or yielding half a page of code for a specific setup with different demands, rendering it impossible for my miserable pythonskills to develop a sufficient (elegant) result.
In python I have a procedure to identify the existence of folderB, but its contents are unknown and varying to me. In light of full disclosure; here is my XYproblem:
#!/usr/bin/python
from glob import glob
path1 =glob('*')
path2 = glob('*/*/')
print path1
print path2
print len(path2)
a = len(path2)
for i in range(0,a):
print len(path2[i]), path2[i], path2[i][len(path2[i])-2]
print(path2[i][len(path2[i])-2].isdigit())
if path2[i][len(path2[i])-3]==" " and path2[i][len(path2[i])-2].isdigit():
print('yay')
newpath =path2[i][:len(path2[i])-2]+"0"+path2[i][(len(path2[i])-2):]
print(newpath)
import os
print(os.path.isdir(newpath))
if os.path.isdir(newpath): #if it is true, the new folder with the " 0n" already exists
import shutil
newpath0=path2[i]+ '*/'
print(newpath0, "headsup", newpath)
shutil.copy(newpath0,newpath)
#shutil.move(
But for the sake of those who seek an efficient, simple solution to this problem please keep it to the simplified, and hypothetical case of "folderX" :)
-edit-
Because the folderA indeed already exists, and has the same (potential) hierarchy as folderB. That is why
shutil.rmtree('folderA')
is not an option; folderA should remain intact.
So, ironically, temporarily removing the contents of folderA to put the contents of folderB in it, yields essentially the exact same problem as I am trying to solve.
-edit2-
Thanks for the effort, I keep on getting:
SyntaxError: invalid syntax
at the
def mv(from_folder, to_folder)
^
Now I appologize for my exceptional level of retardedness but I currently lack the clarity to comprehend how you envisioned your solution;
So after looking up the def mv(.. function I came to the conclusion that you might mean that from_folder should be replaced by a string which contains folderB.
So I tried 2 options:
your original
defining:
stringA='folderA', stringB=folderB
and substituting from_folder with stringB and to_folder with stringA in the first 3 rows of your code.
Both yield the same error.
*Note import os and import shutil have already been performed within the active if loop. Just to ensure that was the not causing the problem, I also tried it with an the given explicit import os and import shutil immediatly above def..
the code I so far have hence looks like:
#!/usr/bin/python
from glob import glob
path1 =glob('*')
path2 = glob('*/*/')
#print path1
#print path2
#print len(path2)
a = len(path2)
for i in range(0,a):
#print len(path2[i]), path2[i], path2[i][len(path2[i])-2]
#print(path2[i][len(path2[i])-2].isdigit())
if path2[i][len(path2[i])-3]==" " and path2[i][len(path2[i])-2].isdigit():
#print('yay')
newpath =path2[i][:len(path2[i])-2]+"0"+path2[i][(len(path2[i])-2):]
#print(newpath)
import os
#print(os.path.isdir(newpath))
if os.path.isdir(newpath): #if it is true, the new folder with the " 0n" already exists
import shutil
newpath0=path2[i]+ '*/'
#print(newpath0, "hier", newpath, ' en path2[i] ', path2[i])
#shutil.copy(newpath0,newpath)
#from here chose from_folder = path2[i] and to_folder=newpath
stringb = path2[i]
stringa = newpath
print(stringb,' ', stringa)
print('reached')
def mv(stringb, stringa):
root_src_dir = stringb
print('reached')
root_dst_dir = stringa
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir)
if not os.path.exists(dst_dir):
os.mkdir(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
os.remove(dst_file)
shutil.move(src_file, dst_dir)
But it refuses to print the second "reached", or in other words, it does not define

import shutil
shutil.copytree('folderB', 'folderA')
Just make sure folderA doesn't exist before running the command.
If for some reason the folder already exists beforehand and you have no control over that, just do
shutil.rmtree('folderA')
shutil.copytree('folderB', 'folderA')

Thanks for all the help! I've tried a different approach;
#!/usr/bin/python
from glob import glob
path1 =glob('*')
path2 = glob('*/*/')
print path1
print('hi')
#print path2
#print len(path2)
a = len(path2)
for i in range(0,a):
#print len(path2[i]), path2[i], path2[i][len(path2[i])-2]
#print(path2[i][len(path2[i])-2].isdigit())
if path2[i][len(path2[i])-3]==" " and path2[i][len(path2[i])-2].isdigit():
#print('yay')
newpath =path2[i][:len(path2[i])-2]+"0"+path2[i][(len(path2[i])-2):]
#print(newpath)
import os
#print(os.path.isdir(newpath))
if os.path.isdir(newpath): #if it is true, the new folder with the " 0n" already exists
import shutil
newpath0=path2[i]+ '*/'
#print(newpath0, "hi", newpath, ' en path2[i] ', path2[i])
#shutil.copy(newpath0,newpath)
#from here chose from_folder = path2[i] and to_folder=newpath
#stringb = path2[i]
#stringa = newpath
#print(stringb,' ', stringa)
print('reached')
subb = os.listdir(path2[i]) #subb is defined here as a list of all the subfolders and subfiles in folderB
#print(subb,len(subb))
print(newpath) #newpath = folderA
for c in range(0,(len(subb))): #This creates an index running for running through all the entries in subb
completesubb=range(len(subb)) #This line defines an array which will contain the full path to all the subfolders and subfiles within folderB
completesubb[c] = path2[i]+subb[c]#here the full path to the subfolders and subfiles within folderB is created,(with 1 sub-file/folder per entry in the array)
print completesubb[c]
completesuba=range(len(subb)) #This will not be required
#move_file(completesubb[c],newpath) #gave an error
shutil.move(completesubb[c],newpath) #moves everythin inside folderB to inside folderA
Now all that is left for me to do is improve the recognizing procedure for "folderA" and folderB but that's another story :) Thnx everyone, as soon as my rep reaches 15 you'll get an automated raise ;)

Try this
import os
import shutil
def mv(from_folder, to_folder):
root_src_dir = from_folder
root_dst_dir = to_folder
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir)
if not os.path.exists(dst_dir):
os.mkdir(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
os.remove(dst_file)
shutil.move(src_file, dst_dir)
Then
>>>mv('path/to/folderB', 'path/to/folderA')

Related

How to extract a path if it contains a folder

Lets say some paths like these:
C:/Test/path_i_need/test2/test3/test4
C:/Test/test2/path_i_need/test3
C:/Test/test2/test3/path_i_need/test4
How I can extract the path that i need in each of the scenarios using python, for example:
C:/Test/path_i_need
C:/Test/test2/path_i_need
C:/Test/test2/test3/path_i_need
So basically i don't know how many sub folder are before the path_i_need or after it, I only need that path, i dont care whats after.
You could do a DFS (depth-first search) from the root directory until you find all the paths you're looking for:
from os import listdir, path
ROOT_DIR = "./example"
FLAG = "example1"
found_dirs = []
def find_dirs(p):
subdirs = listdir(p)
for subdir in subdirs:
curdir = path.join(p, subdir)
if subdir == FLAG:
found_dirs.append(curdir)
elsif path.isdir(curdir):
find_dirs(curdir)
find_dirs(ROOT_DIR)
Try this, without using os module or any imports:
paths = """
C:/Test/path_i_need/test2/test3/test4
C:/Test/test2/path_i_need/test3
C:/Test/test2/test3/path_i_need/test4
""".strip().split('\n')
need_this_path = 'path_i_need'
len_that_which_i_need = len(need_this_path)
extracted_paths = [p[:p.index(need_this_path) + len_that_which_i_need] for p in paths]
print(*extracted_paths, sep='\n')
Outputs:
C:/Test/path_i_need
C:/Test/test2/path_i_need
C:/Test/test2/test3/path_i_need

Python compare filename with folder name

I just start python and i have to compare filename with folder name to launch the good sh script. (i'm using airflow)
import glob
import os
import shutil
from os import path
odsPath = '/apps/data/02_ODS/'
receiptPath = '/apps/data/80_DATA/01_Receipt/'
for files in os.listdir(receiptPath):
if(files.startswith('MEM_ZMII') or files.startswith('FMS') and files.endswith('.csv')):
parsedFiles = files.split('_')
pattern = '_'.join(parsedFiles[0:2])
fileName = '_'.join(parsedFiles[2:5])
fileName = fileName.split('-')[0].lower()
# print('appCode: ', pattern)
# print('fileName: ', fileName)
for odsFolder in os.listdir(odsPath):
if(odsFolder == fileName):
print('it exist: ', str(fileName))
else:
print('it\'s not')
I got 3 files in receiptPath , it only matching for 1 file, but not the others. Can someone help me?
Thank a lot!
Ok, your problem is that you overwrite your variable fileName, so at the end of the first for loop, it only keeps the last value, which is material_makt. The solution consists in saving all the filenames in a list fileNames_list, and then you can check if (odsFolder in fileNames_list) :
import glob
import os
import shutil
from os import path
odsPath = '/apps/data/02_ODS/'
receiptPath = '/apps/data/80_DATA/01_Receipt/'
fileNames_list = []
for files in os.listdir(receiptPath):
if(files.startswith('MEM_ZMII') or files.startswith('FMS') and files.endswith('.csv')):
parsedFiles = files.split('_')
pattern = '_'.join(parsedFiles[0:2])
fileName = '_'.join(parsedFiles[2:5])
fileName = fileName.split('-')[0].lower()
fileNames_list.append(fileName)
for odsFolder in os.listdir(odsPath):
if (odsFolder in fileNames_list):
print('it exist:', str(odsFolder))
else:
print('it\'s not')
Output :
it exist: zcormm_familymc
it exist: kpi_obj_data
it exist: material_makt

Strange behaviour with os.join()

I've noticed odd behaviour with Pythons os.join(). in that I'm adding a year and filename to a path. Here's my code.
#!/usr/bin/env python
import os
#------------------------------------------------
def file_walk(root, ext):
# Walk file with me, Laura Palmer!
fList = []
for current, dirs, files in os.walk(root):
for file in files:
fname = os.path.join(current, file) # this works fine, yeah!
src = os.path.isfile(fname)
if src:
if fname.endswith(ext):
fList.append(fname)
return fList
myFolder = r"d:\temp\test"
myExt = ".html"
myYear = "2019"
allfiles = file_walk(myFolder, myExt)
for theFile in allfiles:
sourceFile = theFile
destinFile = os.path.join(myFolder, myYear, theFile)
print sourceFile
print destinFile
print
myFile = "bookmarks_06_05_2019.html"
print os.path.join(myFolder, myYear, myFile)
# EoF
As strings, they work fine (see last line), but as paths, not so well :(
Output I'm getting from print destinFile
d:\temp\test\bookmarks_01_26_2018.html
d:\temp\test\bookmarks_05_06_2014.html
d:\temp\test\bookmarks_06_05_2019.html
I'm expecting the follow:
d:\temp\test\2019\bookmarks_01_26_2018.html
d:\temp\test\2019\bookmarks_05_06_2014.html
d:\temp\test\2019\bookmarks_06_05_2019.html
Can anyone point me in the right direction of where I'm going wrong?
theFile is an absolute file path. If all you want from it is the base name, use:
destinFile = os.path.join(myFolder, myYear, os.path.basename(theFile))
Note that os.path.join returns the last absolute argument with any relative arguments after that one combined in a path. This is why the result didn't have the 2019 component.

python move file based on dictionary

I'm trying to move specific files(only ending with *.txt) from folders under /home/wiet/import/ directory(recursive) search. But I want to do it based on the dictionary-so for example in folder /home/wiet/import/OS only files with ending *windows.txt, or *rhel.txt should be moved-about other files I want information in console about not matching files(and preferably add them to some list). For now I've got this one below, but I cannot match it with dictionary and I've got problem with copying correct files to another directory
import sys
import os
import shutil
import glob
import fnmatch
tech = {"OS": ["rhel.txt", "windows.txt"]
"DB" : ["mysql.txt","oracle.txt","postgres.txt","postgresql.txt"],
"WEB" : ["iis.txt" , "apache.txt", "tomcat.txt" ] ,
}
fileslist = []
txtcorrect = []
destin = "/home/wiet/import"
print "part1 \n \n \n"
path = os.path.join(destin)
for path, subdirs, files in os.walk(destin):
for name in files:
if name.endswith('.txt'):
print "found: " + name
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, '*.txt'):
txtcorrect.replace(os.path.join(root, filename))
print txtcorrect
shutil.copy2( txtcorrect , '/home/wiet/out')
I'm not sure what you want.
Hope this helps:
....
....
for key in tech:
if name in tech[key]:
'''do something'''
....
....

Pulling Files and Timestamps from a Directory and Subdirectories

I have a working script that will print all files in a given directory. I would like help making it do two additional things:
(1) Also be able to print the date_created or time stamp for each file.
(2) Do all of the above not only for files in the given directory, but in all subdirectories as well.
Here is the working script:
from os import listdir
from os.path import isfile, join
from sys import argv
script, filename = argv
mypath = os.getcwd()
allfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
output = open(filename, 'w')
for i in allfiles:
string = "%s" %i
output.write(string + "\n")
output.close()
print "Directory printed."
I would hope to be able to print something like (filename + ", " + timestamp + "\n"), or some substitute.
Thanks!
http://docs.python.org/2/library/os.html and http://docs.python.org/2/library/stat.html have you covered.
os.walk will give you the recursive directory walking
stat will give you file timestamps (atime,ctime,mtime)
This snippet walks through files in a directory + subdirectories and prints out created and modified timestamps.
import os
import time
def walk_files(directory_path):
# Walk through files in directory_path, including subdirectories
for root, _, filenames in os.walk(directory_path):
for filename in filenames:
file_path = root + '/' + filename
created = os.path.getctime(file_path)
modified = os.path.getmtime(file_path)
# Process stuff for the file here, for example...
print "File: %s" % file_path
print " Created: %s" % time.ctime(created)
print " Last modified: %s" % time.ctime(modified)
walk_files('/path/to/directory/')

Categories

Resources