os.walk Python can't print all drivers - python

Im using os module to get all the files in a directory
drives = [ chr(x) + ":\" for x in range(65,91) if os.path.exists(chr(x) + ":\") ]
print(drives)
prints out
['C:\', 'D:\']
then I look for all the files in those disks with
counter = 0
inp = '.png'
thisdir = os.getcwd()
for r, d, f in os.walk(drives[0-1]):
for file in f:
filepath = os.path.join(r, file)
if inp in file:
counter += 1
print(os.path.join(r, file))
print(f"counted {counter} files.")
but I get only D:\ drives '.png' file's which print's out 55.000 of pictures location on the drive I cant get c:\ what am I doing here wrong I'm a bit python newbie right now so I don't know what to do can someone help me please?

To reply to your comment, this is how you'd loop over drive:
counter = 0
inp = '.png'
for drive in drives:
for r, d, f in os.walk(drive):
for file in f:
filepath = os.path.join(r, file)
if inp in file:
counter += 1
print(os.path.join(r, file))
print(f"counted {counter} files.")

Related

Rename substring of actual files - python

I am reading in all the files in a given folder:
import os
path = '/Users/user/Desktop/folder_name'
files = os.listdir(path)
I have multiple files (100+) with the following names:
20220330_a.txt 20220330_b.txt 20220330_c.txt
I want to replace the "20220331" to "20220630" in the actual file names in the folder, so I obtain 20220630_a.txt, 20220630_b.txt etc.
Any ideas?
I figured it out myself:
old_date = "20200331"
new_date = "20200630"
for file in os.listdir(path):
if file.startswith(old_date):
if file.find(old_date) > -1:
counter = counter + 1
os.rename(os.path.join(path, file), os.path.join(path, file.replace(old_date,new_date)))
if counter == 0:
print("No file has been found")

Script that should remove certain txt files from a directory does not remove the files

I am trying to write a python script that iterates over all the txt files in a directory and deletes those that have fewer words than a defined limit. The current script does not delete the files. Below is what I tried:
import os
wordLimit = 1000
def articleWordCount(filename):
number = 0
with open(filename, 'w+') as f:
for line in f:
words = line.split()
number += len(words)
return number
def articlesRemoval():
directory = 'data'
removedArticles =0
for filename in os.listdir(directory):
if filename.endswith(".txt"):
if articleWordCount(filename) < wordLimit:
removedArticles += 1
os.remove(filename)
else:
print(os.path.join(directory, filename))
print(removedArticles)
articlesRemoval()
You should open the file in reading mode with the option "r+", you are opening it in write mode and the function articleWordCount always returns 0.
os.listdir() doesn't return paths, only filenames, so the files that you are trying to remove do not exist... I am assuming that data is in the directory where you are starting the script and that it does find the files you want. Change os.remove(filename) to os.remove(os.path.join(directory, filename)):
import os
wordLimit = 1000
def articleWordCount(filename):
number = 0
with open(filename, 'w+') as f:
for line in f:
words = line.split()
number += len(words)
return number
def articlesRemoval():
directory = 'data'
removedArticles = 0
for filename in os.listdir(directory):
if filename.endswith(".txt"):
if articleWordCount(filename) < wordLimit:
removedArticles += 1
os.remove(os.path.join(directory, filename))
else:
print(os.path.join(directory, filename))
print(removedArticles)
articlesRemoval()

Python: How make a zip with file, not into folder

Folder contain my files and I want to make a zip with those files, and save zip into a folder.
Here my files
- file:
- file_0.txt
- file_1.txt
- file_2.txt
- zip:
// save zip
script.py
Here my code
from zipfile import ZipFile
zip_name = "Zipfile"
zipObj = ZipFile("zip/{}.zip".format(zip_name), "w")
count = 0
while count < 3:
file_name = "file_"
zipObj.write('file/' + file_name + str(count) + ".txt")
count += 1
This make a Zip file with a folder named file, and inside all txt, I want to remove folder and only zip the files
from zipfile import ZipFile
zip_name = "Zipfile"
zipObj = ZipFile("zip/{}.zip".format(zip_name), "w")
count = 0
import os
os.chdir('file')
while count < 3:
file_name = "file_"
zipObj.write(file_name + str(count) + ".txt")
count += 1
This should work for you

Get all files from my C drive - Python

Here is what I try to do:
I would like to get a list of all files that are heavier than 35 MB in my C drive.
Here is my code:
def getAllFileFromDirectory(directory, temp):
files = os.listdir(directory)
for file in files:
if (os.path.isdir(file)):
getAllFileFromDirectory(file, temp)
elif (os.path.isfile(file) and os.path.getsize(file) > 35000000):
temp.write(os.path.abspath(file))
def getFilesOutOfTheLimit():
basePath = "C:/"
tempFile = open('temp.txt', 'w')
getAllFileFromDirectory(basePath, tempFile)
tempFile.close()
print("Get all files ... Done !")
For some reason, the interpreter doesn't go in the if-block inside 'getAllFileFromDirectory'.
Can someone tell me what I'm doing wrong and why (learning is my aim). How to fix it ?
Thanks a lot for your comments.
I fixed your code. Your problem was that os.path.isdir can only know if something is a directory if it receives the full path of it. So, I changed the code to the following and it works. Same thing for os.path.getsize and os.path.isfile.
import os
def getAllFileFromDirectory(directory, temp):
files = os.listdir(directory)
for file in files:
if (os.path.isdir(directory + file)):
if file[0] == '.': continue # i added this because i'm on a UNIX system
print(directory + file)
getAllFileFromDirectory(directory + file, temp)
elif (os.path.isfile(directory + file) and os.path.getsize(directory + file) > 35000000):
temp.write(os.path.abspath(file))
def getFilesOutOfTheLimit():
basePath = "/"
tempFile = open('temp.txt', 'w')
getAllFileFromDirectory(basePath, tempFile)
tempFile.close()
print("Get all files ... Done !")
getFilesOutOfTheLimit()

Open pdf files format with python

I try to open 100 pdf files with python 2.7 with this code:
import arcpy,fnmatch,os
rootPath = r"D:\desktop"
pattern = '*.pdf'
counter = 0
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
os.startfile(rootPath)
counter = counter + 1
print counter
as a result the rootPath folder opened and python print the number of pdf files:
>>>
39
>>>
No pdf files opened. I search in the forum and didn't find any question with answers to my request. Thanks for any help
I don't know what are you trying to do, but os.startfile will open up adobe pdf reader (or any other reader that's set as default reader)... here how i managed to do that and it seems to be working.
import os
rootPath = "D:\\desktop"
counter = 0
for file in os.listdir(rootPath):
if file.endswith('.pdf'):
os.startfile("%s/%s" %(rootPath, file))
counter = counter + 1
print counter
or without much editing your main code
import arcpy,fnmatch,os
rootPath = r"D:\desktop"
pattern = '*.pdf'
counter = 0
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
os.startfile("%s/%s" %(rootPath,filename))
counter = counter + 1
print counter
Your are always calling
os.startfile(rootPath)
where rootPath is only "D:\desktop". You must call os.startfile with the path to the PDF file as the argument.
os.startfile("{}/{}".format(rootPath, file))

Categories