Scons Copying File on Changes? - scons

Well i have some extra text files, with different extensions, and i need them to be copied to the bin. Right now i am using:
files = []
for root, dirs, files in os.walk("extra_src"):
for file in files:
files.append(["extra_src" + os.sep + file, "bin" + os.sep + file])
for element in files:
command = Command(target = element[1], source = element[0], action = Copy("$TARGET", "$SOURCE"))
Requires(program, command)
Is there any other way to get it to register the files and simply specify all the files in a said directory? I can use Command(..., Copy("dir1", "dir2")) but it doesn't detect changes, and doesn't clean out the bin of those files.

Try something along the lines of:
import os # for os.path.join
inst = env.Install('bin', Glob(os.path.join('extra_src','*.*')))
env.Depends(program, inst) # if required
Note, how the Glob() function will even find files that don't exist yet, but get created by another build step.

Related

How to create a file in python having name = ".gitignore"

I was trying to create a file without a name in python (only filetype)
I tried this -
open(".gitignore","w+").close()
But it does not work.
edit - it does work real issue is in getting file through glob.glob
classify_folder_name = #path of the folder which contain .gitignore file
rel_paths = glob.glob(classify_folder_name + '/**', recursive=True)
for local_file in rel_paths:
print(local_file)
it does not print .gitignore file.
Any help will be appreciated.
Note -: don't want to use os.listdir()
There are few things that you might check:
files with dot at the beginning are hidden so whatever OS you are using, make sure you have hidden files visibility enabled
It might be saved in different directory
open(".gitignore","w+").close()
It would be better if you do this:
To create a file:
with open('.gitignore', 'w') as fp:
pass

how to move files from one folder location to other using python

I am creating a automation script and my requirement is to move some files from one folder to another and get it renamed in the meanwhile
I have tried using shutil and os module but none helped me so far
src = r'C:\\Users\\XX\\Downloads\\'
dst = r'C:\\Users\\XX\\Documents\\UIPATH_DUMP\\'
regex = re.compile('MSS_')
files = os.listdir(src)
for i in files:
if regex.match(i):
src1 = src + i
dst1 = dst + i
shutil.move(src1, dst1)
The expected result is my file should get moved to the destination location. I am not able to figure out just how will I rename it? maybe os.rename() would work?
You can use os.rename() to move the file to another path as well as rename it.
For example, if the original file is:
"/Users/billy/d1/xfile.txt"
and you would like to move it to folder "d2" and name it "yfile.txt", you can use the following line of code:
os.rename('/Users/billy/d1/xfile.txt', '/Users/billy/d2/yfile.txt')

Multitasking in Python, Reformat and Move

I have a many folders. I want to make .zip format of files and move them to another directory:
for (dir, dirs, files) in os.walk(directory):
for filename in files:
# make zip files
if filename.endswith(".zip"):
# move created zip files
os.rename(dir + '/' + filename, new + '/' + directory + '/' + filename)
The problem is that, after make the zip file, program couldn't find the created file with zip extension. I want to create a zip format and move it to another folder.
You can use the shutil lib:
import shutil
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
shutil.move(src, dst)
Recursively move a file or directory (src) to another location (dst).
If the destination is an existing directory, then src is moved inside
that directory. If the destination already exists but is not a
directory, it may be overwritten depending on os.rename() semantics.
If the destination is on the current filesystem, then os.rename() is
used. Otherwise, src is copied (using shutil.copy2()) to dst and then
removed.
Also you can use shutil for archive file:
shutil.make_archive(base_name, format[, root_dir[, base_dir[,
verbose[, dry_run[, owner[, group[, logger]]]]]]])
Create an archive
file (such as zip or tar) and return its name.
For more info read shutil documentation

Python: Delete a special file

you know special files or folders where the first place is a point. For example: .example_folder or .examaple_file. Think about the .htaccess/.htpassword files. I know how to delete folders and files on the regular way with Python. But how can I delete some special files like this? The other problem is the special files don't have extensions like .txt oder .jpg etc.. When I try to delete the special files/folders on the regular way Python skips all the special files/folder. Does someone has any idea?
def delete_temp_update_files(path_files):
files = glob.glob(path_files)
for f in files:
os.remove(f)
def delete_temp_update_folders(folder_path, path_files):
folder_paths = glob.glob(folder_path)
'''
First, all folders are deleted in this current folder (folder_paths).
'''
if not folder_paths:
'''
The list (folder_paths) is empty, that means there aren't somer folders.
In this case its enough to delete all files - everything including
hidden files.
'''
result_files = delete_temp_update_files(path_files)
return result_files
else:
'''
There are some folders. Before the files are deleted, the folder must be deleted.
'''
for folder_element in folder_paths:
shutil.rmtree(folder_element, ignore_errors=True)
'''
Now all folders are delete have been deleted. Netx all files should be deleted.
'''
result_files = delete_temp_update_files(path_files)
return result_files
def on_delete_files_folders(update_temp_zip_file):
# The variable named (all_files) shows all files - everything including hidden files
all_files = os.path.join(update_temp_zip_file, '*')
# The variable named (all_folders) shows all folders in current folder.
all_folders = os.path.join(update_temp_zip_file, '*/')
delete_temp_update_folders(all_folders, all_files)
on_delete_files_folders("PATH/TO/YOUR/FOLDER")
Your decision is rather sophisticated.
You may use my script below:
import os
def del_recursive(path):
for file in os.listdir(path):
file = os.path.join(path, file)
if os.path.isdir(file):
try:
os.rmdir(file)
except:
del_recursive(file)
os.rmdir(file)
else:
os.remove(file)
If you want to delete everything in a directory you also may use shutil.rmtree(). Both variants work on Python 2.7.3.

Replacing files in one folder and all its subdirectories with modified versions in another folder

I have two folders, one called 'modified' and one called 'original'.
'modified' has no subdirectories and contains 4000 wav files each with unique names.
The 4000 files are copies of files from 'original' except this folder has many subdirectories inside which the original wav files are located.
I want to, for all the wav files in 'modified', replace their name-counterpart in 'original' wherever they may be.
For example, if one file is called 'sound1.wav' in modified, then I want to find 'sound1.wav' in some subdirectory of 'original' and replace the original there with the modified version.
I run Windows 8 so command prompt or cygwin would be best to work in.
As requested, I've written the python code that does the above. I use the 'os' and 'shutil' modules to first navigate over directories and second to overwrite files.
'C:/../modified' refers to the directory containing the files we have modified and want to use to overwrite the originals.
'C:/../originals' refers to the directory containing many sub-directories with files with the same names as in 'modified'.
The code works by listing every file in the modified directory, and for each file, we state the path for the file. Then, we look through all the sub-directories of the original directory, and where the modified and original files share the same name, we replace the original with the modified using shutil.copyfile().
Because we are working with Windows, it was necessary to change the direction of the slashes to '/'.
This is performed for every file in the modified directory.
If anyone ever has the same problem I hope this comes in handy!
import os
import shutil
for wav in os.listdir('C:/../modified'):
modified_file = 'C:/../modified/' + wav
for root, dirs, files in os.walk('C:/../original'):
for name in files:
if name == wav:
original_file = root + '/' + name
original_file = replace_file.replace('\\','/')
shutil.copyfile(modified_file, original_file)
print wav + ' overwritten'
print 'Complete'

Resources