locate some files and append the content in python - python

I am trying to find all the files with name ACC.txt in a given folder and its subfolder and then append them to create a new file i.e. output_file.txt. All the files are not in the working directory.
I have written the following code, but it is throwing a empty file.Please correct the code
import os
import shutil
BASE_DIRECTORY = r'E:\equity research\test_data'
with open('output_file.txt','wb') as wfd:
for dirpath, dirnames, filenames in os.walk(BASE_DIRECTORY):
for filename in filenames:
if filename == "ACC.txt":
fullPath = os.path.join(dirpath, filename)
with open(fullPath,'rb') as fd:
shutil.copyfileobj(fd, wfd)

Related

Copy specific files in a all Directory with python

I found out how to do it with this script:
import os, sys, shutil, glob
if not os.path.exists('Files'):
os.makedirs('Files')
source_dir = os.path.join(os.environ["HOMEPATH"], "Desktop")
dest_dir = 'Files'
try:
for root, dirnames, filenames in os.walk(source_dir):
for file in filenames:
(shortname, extension) = os.path.splitext(file)
if extension == ".txt" :
shutil.copy2(os.path.join(root,file), os.path.join(dest_dir,
os.path.relpath(os.path.join(root,file),source_dir)))
except FileNotFoundError:
pass
But, it only copies the files in the path and not the in sub-folders
So for example here it copies the desktop .txt files, but it does not copy the files in the folders.
Thanks, have a good day.

Python: linecache.getline not working as intended

I have a directory with numerous subdirectories.
At the bottom of the directories there are some .txt files i need to extract line 2 from.
import os
import os.path
import linecache
for dirpath, dirnames, filenames in os.walk("."):
for filename in [f for f in filenames if f.endswith(".txt")]:
#print os.path.join(dirpath, filename)
#print filename
print linecache.getline(filename, 2)
I am able to successfully parse all the directories and find every text file. But linecache.getline simply returns newline (where there should be data from that line of the files). Using
print linecache.getline(filename, 2).rstrip('\n')
Does not solve this either.
I am able to correctly print out just the filenames in each directory, but passing these to linecache seems to potentially be the issue. I am able to use linecache.getline(file, lineno.) successfully if I just run the script on 1 .txt file in the current directory.
linecache.getline takes filename from current working directory.
Solution is thus:
import os
import os.path
import linecache
for dirpath, dirnames, filenames in os.walk("."):
for filename in [f for f in filenames if f.endswith(".txt")]:
direc = os.path.join(dirpath, filename)
print linecache.getline(direc, 2)

Trying renaming all files in a folder

I am trying the script below to rename all files in a folder.It is working fine,But when i am trying to run it outside the folder.It shows error.
import os
path=os.getcwd()
path=os.path.join(path,'it')
filenames = os.listdir(path)
i=0
for filename in filenames:
os.rename(filename, "%d.jpg"%i)
i=i+1
'it' is the name of the folder in which files lie.
Error:FileNotFoundError: [Errno 2] No such file or directory: '0.jpg' -> '0.jpg'
Print is showing names of files
When you do os.listdir(path) you get the filenames of files in the folder, but not the complete paths to those files. When you call os.rename you need the path to the file rather than just the filename.
You can join the filename to its parent folder's path using os.path.join.
E.g. os.path.join(path, file).
Something like this might work:
for filename in filenames:
old = os.path.join(path, filename)
new = os.path.join(path, "%d.jpg"%i)
os.rename(old, new)
i=i+1
You need to mention complete or relative path to file.
In this case, it should be
path + '/' + filename
or more generally,
newpath = os.path.join(path, filename)

copying files from one folder to another

I'm trying to make a script that copies all the files other than the zipped files from a source folder to another destination folder and extract zipped files from the source folder to the destination, this is what i have come till this far:
import os
import zipfile
import shutil
myPath = "Source dir"
for root, dirs, files in os.walk(myPath):
for file in files:
if file.endswith('.zip'):
fh = open(file, 'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
outpath = "destination dir"#Put the name of the destination folder
z.extract(name, outpath)
fh.close()
else:
fileList = os.listdir('source dir')
for f in fileList:
shutil.copy2(f, 'destination directory')
The code shows no error but there is no output too.
From Python Standard Library To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name) so you should write
fh = open(so.path.join(root,file))
to have the correct path.

Create a zip file from a directory, but not his fullpath

Im trying to create a zipfile with the content of a folder ( some dirs and files ) using the code belllow:
import zip,os
path = "c:\\tester\\folderToZip"
zip = zipfile.ZipFile("zippedFolder.zip", "w")
for root, dirs, files in os.walk(path):
for file in files:
zip.write(os.path.join(root, file))
zip.close()
But after the code runs, when i check the zip file, the zip file, instead having the content of the folder "folderToZip" ( ex: f1,f2,f2, a.txt,b.txt,c.txt ) it have the full path of the variable path.
So, the question is, how can i create a zipfile based on a folder content, but not his fullpath ?
write takes a second optional parameter arcname, which when specified will provide the name for the file stored. Use that to specify the name you want.
If you only want the filename:
import zip,os
path = "c:\\tester\\folderToZip"
zip = zipfile.ZipFile("zippedFolder.zip", "w")
for root, dirs, files in os.walk(path):
for file in files:
filename = os.path.join(root, file)
zip.write(filename, filename)
zip.close()

Categories

Resources