Python: How can I open a folder with variable - python-2.7

question again... I have a list A=['AA','BB','CC'] and a folder path like ./ABC/. There are three sub folder inside called 032_AA, 0244_BB, 01_CC
format is like random number_AA(BB or CC).
now I tend to use this list to enter these folder and open a txt file which in there sub folder:
cmd1='cd ABC'
os.sys(cmd1)
for i in A:
???????? ---------------------- enter folder 032_AA according to list A
with open('xxx.txt','a') as f:
XXXXXXXX
my main question is I dont know how to enter a folder with a known file name with random number as begining.
So any idea? Thanks!

you can use glob:
import glob
regex = './ABC/*'
for i in A:
subdir = regex + i
for name in glob.glob(subdir):
U can check glob for more info on pattern matching

Related

select some of (EOF) extension files in specific folder based on part of filenames in text file :explained in image file with connected arrow

I want to Select files in specific folder based on part of the name of files(date)
so:
1-first: (in names.csv) extract part of file name(date) :find all("1SDV_(\d+)T", name)
2-second: (in name of files in specific folder) extract part of file name(date):
find all ("OPOD__(\d+)T", name)
of all (EOF) extension file names in specific folder
3-third: relate extraction: as image that I attached (red connected arrow)
my code is not complete please help me to complete it
enter code here
import OS
import re
# open the file, make a list of all filenames, close the file
with open('names.csv') as name file:
# use .strip() to remove trailing whitespace and line breaks
names= [line strip() for line in name file]
for name in names:
res = re find all ("1SDV_(\d+)T", name)
if not res: continue
print res[0]
# You can append the result to a list
# Find all ("OPOD__(\d+)T")
# how can I relate (EOF) extension files name to res like image file I
attached
# move the selected (EOF) extension file
OS rename(OS path join('path', (EOF) file extension folder),
'/path/to/somewhere/else')
break
csv or text file is like this:
S1A_IW_SLC__1SDV_20190826T022837_20190826T022904_028734_0340DD_654D-SLC
S1A_IW_SLC__1SDV_20190919T022838_20190919T022905_029084_034D09_0129-SLC
S1A_IW_SLC__1SDV_20191013T022839_20191013T022906_029434_03590E_A824-SLC
S1A_IW_SLC__1SDV_20191106T022839_20191106T022906_029784_036538_06CC-SLC
S1A_IW_SLC__1SDV_20191130T022838_20191130T022905_030134_037166_4019-SLC
S1A_IW_SLC__1SDV_20191224T022837_20191224T022904_030484_037D7B_0FC4-SLC
S1A_IW_SLC__1SDV_20210217T062720_20210217T062747_036626_044D90_4570-SLC
.
.
.
And (EOF) extension files in specific folder are like :
S1A_OPER_AUX_POEORB_OPOD_20190915T120743_V20190825T225942_20190827T005942.EOF
S1A_OPER_AUX_POEORB_OPOD_20190916T120658_V20190826T225942_20190828T005942.EOF
S1A_OPER_AUX_POEORB_OPOD_20190917T120653_V20190827T225942_20190829T005942.EOF

I am writing a DXL script to print a modulename with path using a function . I need to remove if the directory has remote in that path

Below module is forming a directory from a path
Checkmodules("Cus", "projectname".cfg", ".*(Cus|Cer Requirements|Cer Speci|ASM|/ASM24e|Specs).*");
The above line forms below folder directory, so I need to modify it when the directory has remote in the path its should be removed entire directory.
Output path
Cus/spec/cer/
cus/val_remote/value - this also needs to be removed
Cus/sys/remote ---- to be removed entire path
You can do a replace (to an empty string) with the following regex combined with the flag g (global=all occurrences) and m (multiline)
^.*remote.*$
In JavaScript this would be :
outputPath.replace(/^.*remote.*$/gm, '')

How to list folders/directories that contain particular pattern files in them in python?

For example, if file contains extension .parq I have to list all the directories present in the folder:
/a/b/c/
/a/b/e/
/a/b/f/
Here I have to list the directories c, e, f which has particular pattern files.
You can use os.walk to traverse over all the files and directories. Then you can do simple pattern matching in the file names (like the example you gave in the question).
import os
for path, subdirs, files in os.walk('.'): #Traverse the current directory
for name in files:
if '.parq' in name: #Check for pattern in the file name
print path
You can either append the path to a list to use it later if you want. If you want to access the full file name you can use os.path.join
os.path.join(path, name)
If you want to access patterns within a file, you can modify the code as below.
import os
for path, subdirs, files in os.walk('.'):
for name in files:
with open(name) as f:
#Process the file line by line
for line in f:
if 'parq' in line:
#If pattern is found in file print the path and file
print 'Pattern found in directory %s' %path,
print 'in file %s' %name
break

How to get a file to be used as input of the program that ends with special character in python

I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something

Python - Counting the number of files and folders in a directory

I've got a python script that deletes an entire directory and its subfolders, and I'd like to print out the number of files and folders removed. Currently, I have found some code from a different question posed 2010, but the answer I receive back is 16... If I right-click on the the folder it states that theres 152 files, 72 folders...
The code I currently have for checking the directory;
import os, getpass
user = getpass.getuser()
copyof = 'Copy of ' + user
directory = "C:/Documents and Settings/" + user
print len([item for item in os.listdir(directory)])
How can I extend this to show the same number of files and folders that there actually are?
To perform recursive search you may use os.walk.
os.walk(top, topdown=True, onerror=None, followlinks=False)
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at
directory top (including top itself), it yields a 3-tuple (dirpath,
dirnames, filenames).
Sample usage:
import os
dir_count = 0
file_count = 0
for _, dirs, files in os.walk(dir_to_list_recursively):
dir_count += len(dirs)
file_count += len(files)
I was able to solve this issue by using the following code by octoback (copied directly);
import os
cpt = sum([len(files) for r, d, files in os.walk("G:\CS\PYTHONPROJECTS")])