Since I am a newbie in Scons, I am finding it difficult to migrate my existing Makefile to Scons.
Background:
I have 50 files in a directory.I want to filter files only with *.cxx extension, that too filenames without string "win32" .
Can somebody suggest an implementation for this logic in Scons :
Makefile Implementation :
WIN32FILTER = $(wildcard *win32*)
CXXOBJS = $(patsubst %.cxx,%.o,$(filter-out $(WIN32FILTER),$(wildcard *.cxx)))
In Scons ,I am trying something like this :
moduleSources = ''
for root, dirs, files in os.walk('./'):
for filename in fnmatch.filter(files, '*.cxx'):
if "win32" not in filename:
moduleSources += ' ' + filename
env.StaticLibrary( "support_host", moduleSources)
moduleSources here should contain list of all *.cxx files (excluding win32 string) which will be used to make a Static library.
Any help is appreciated.
You create a single string with spaces in it to describe the set of source code files. This doesn't do what you hoped.
Instead, create a list of filenames. The following SConstruct does what you want:
import os
import fnmatch
env = Environment()
moduleSources = []
for root, dirs, files in os.walk('./'):
for filename in fnmatch.filter(files, '*.cxx'):
if "win32" not in filename:
moduleSources.append(os.path.join(root, filename))
env.StaticLibrary( "support_host", moduleSources)
Related
I am trying to search through a directory and associated subdirectories to see if these listed jpg files are missing. I have got it looping through one directory but cannot extend the search into any subdirectories.
I have tried using os.walk but it just loops through all files and repeats that all files are missing even if they are not. So I am not sure how to proceed.
This is the code that I have so far.
source = 'path_to_file'
paths = ['Hello', 'Hi', 'Howdy']
for index, item in enumerate(paths):
paths[index] = (source + '\\' + paths[index]+'.jpg')
mp = [path for path in paths if not isfile(path)]
for nl in mp:
print(f'{nl}... is missing')
As you told that using os.walk you were unable to get your desired output, Here's a solution.
What i have done is using os os.walk i have searched the whole directory and then appended the file names to a list called emty_list. Then i have tried to check if the item in the list file_name is in emty_list or not.
import os
source = r'path'
emty_list=[]
file_name= ['hello.jpg', 'Hi.jpg', 'Howdy.jpg']
for root, dirs, files in os.walk(f"{source}", topdown=False): #Listing Directory
for name in files:
emty_list.append(name)
for check in file_name:
if check not in emty_list:
print(f"File Not Found Error : File Name: {check}")
Note: Please check if the file that you have created in your system is for example Hello.jpg not hello.jpg.
You can leverage glob and the recursive parameter to do that in python :
import glob
source='./'
paths=['file1','file2','file3']
for path in paths:
print(f"looking for {path} with {source+'**/'+path+'.jpg'}")
print(glob.glob(source+"**/"+path+".jpg",recursive=True))
mp=[path for path in paths if not glob.glob(source+"**/"+path+".jpg",recursive=True)]
for nl in mp:
print(f'{nl}... is missing')
(You can the remove the for loop line 5-7, it's just to clarify how glob works, the comprehension list itself is enough)
With the following folder :
.
├── file1.jpg
├── search.py
└── subfolder
└── file3.jpg
It returns :
looking for file1 with ./**/file1.jpg
['./file1.jpg']
looking for file2 with ./**/file2.jpg
[]
looking for file3 with ./**/file3.jpg
['./subfolder/file3.jpg']
file2... is missing
I'm attempting to rename multiple files in a github repo directory on windows 10 pro
The file extensions are ".pgsql" (old) and ".sql" (rename to)
I'm using vscode (latest) and python 3.7 (latest)
I can do it, one folder at a time, but whenever I have tried any recursive directory code I've looked up on here I cant get it to work.
Currently working single directory only
#!/usr/bin/env python3
import os
import sys
folder = 'C:/Users/YOURPATHHERE'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.pgsql', '.sql')
output = os.rename(infilename, newname)
I'd like to have it recursively start in a directory and change only the file extensions specified to .sql in all sub directories as well on windows, for example
folder = 'C:/Users/username/github/POSTGRESQL-QUERY/'
You can use os.walk(),
import os
folder = 'C:/Users/YOURPATHHERE'
for root, dirs, files in os.walk(folder):
for filename in files:
infilename = os.path.join(root,filename)
newname = infilename.replace('.pgsql', '.sql')
output = os.rename(infilename, newname)
My directory contains several folders, each with several subdirectories of their own. I need to move all of the files that contain 'Volume.csv' into a directory called Volume.
Folder1
|---1Area.csv
|---1Circumf.csv
|---1Volume.csv
Folder2
|---2Area.csv
|---2Circumf.csv
|---2Volume.csv
Volume
I'm trying combinations of os.walk and regex to retrieve the files by filename but not having much luck.
Any ideas?
Thank you!
Sunworshipper, thank you for the answer!
I ran the following code and it moved the entire directory rather than just file name containing 'Volume'. Is it clear why that happened?
import os
import shutil
source_dir = "~/Stats/"
dest_dir = "~/Stats/Volume/"
file_paths = set()
for dir_, _, files in os.walk(source_dir):
for fileName in files:
if "Volume" in fileName:
relDir = os.path.relpath(dir_, source_dir)
file_paths.add(relDir)
for matched in file_paths:
shutil.move(matched, dest_dir)
You can use glob for this. It returns a list of path names matching the expression you give it.
import glob
import shutil
dest = 'testfiles/'
files = glob.glob('*/*test.csv')
for file in files:
shutil.move(file, dest)
I used relative paths but you can also use absolute paths.
shutil moves the documents to the new location. See the glob.glob documentation for more info.
import os
import shutil
Setup your source and destination directories
source_dir = "/Users/nenad/Documents/Python Files/Random Tests"
dest_dir = "/Users/nenad/Documents/Python Files/Random Tests/volume"
This set will now hold paths of all files matching your substring.
file_paths = set()
Now I only consider the directories that contain a file which has a substring "hello" in the filename.
for dir_, _, files in os.walk(source_dir):
for fileName in files:
if "hello" in fileName:
relDir = os.path.relpath(dir_, source_dir)
relFile = os.path.join(relDir, fileName)
file_paths.add(relFile)
And now you just move them to your destination with shutil.
for matched in file_paths:
shutil.move(matched, dest_dir)
Sorry for the misread :)
Best regards
I have a directory called /user/local/ inside which i have several files of the form, jenjar.dat_1 and jenmis.dat_1. There is another directory /user/data inside which there are two subdirectories of the form, jenjar and jenmis. I need a Python code that would move the jenjar.dat_1 into the jenjar directory of /user/data and similarly, jenmis.dat_1 into jenmis directory of '/user/data.
I guess the os module would work for thus but I'm confused. Most of the questions here do not show a Pythonic way to do this.
EDIT: I have found the solution to this
destination = '/user/local'
target = '/user/data'
destination_list = os.listdir(destination)
data_dir_list = os.listdir(target)
for fileName in destination_list:
if not os.path.isdir(os.path.join(destination, fileName)):
for prefix in data_dir_list:
if fileName.startswith(prefix):
shutil.copy(os.path.join(destination, fileName), os.path.join(target, prefix, fileName))
This should do the trick
srcDir = '/user/local'
targetDir = '/user/data'
for fname in os.listdir(srcDir):
if not os.path.isdir(os.path.join(srcDir, fname)):
for prefix in ['jenjar.dat', 'jenmis.dat']:
if fname.startswith(prefix):
if not os.path.isdir(os.path.join(targetDir, prefix)):
os.mkdir(os.path.join(targetDir, prefix))
shutil.move(os.path.join(srcDir, fnmae), targetDir)
I have some files that I'm working with in a python script. The latest requirement is that I go into a directory that the files will be placed in and rename all files by adding a datestamp and project name to the beginning of the filename while keeping the original name.
i.e. foo.txt becomes 2011-12-28_projectname_foo.txt
Building the new tag was easy enough, it's just the renaming process that's tripping me up.
Can you post what you have tried?
I think you should just need to use os.walk with os.rename.
Something like this:
import os
from os.path import join
for root, dirs, files in os.walk('path/to/dir'):
for name in files:
newname = foo + name
os.rename(join(root,name),join(root,newname))
I know this is an older post of mine, but seeing as how it's been viewed quite a few times I figure I'll post what I did to resolve this.
import os
sv_name="(whatever it's named)"
today=datetime.date.today()
survey=sv_name.replace(" ","_")
date=str(today).replace(" ","_")
namedate=survey+str(date)
[os.rename(f,str(namedate+"_"+f)) for f in os.listdir('.') if not f.startswith('.')]
import os
dir_name = os.path.realpath('ur directory')
cnt=0 for root, dirs, files in os.walk(dir_name, topdown=False):
for file in files:
cnt=cnt+1
file_name = os.path.splitext(file)[0]#file name no ext
extension = os.path.splitext(file)[1]
dir_name = os.path.basename(root)
try:
os.rename(root+"/"+file,root+"/"+dir_name+extension)
except FileExistsError:
os.rename(root+"/"+file,root+""+dir_name+str(cnt)+extension)
to care if more files are there in single folder and if we need to give incremental value for the files