I am facing simple problem, but can't get my head around it.
I have millions of millions files, in millions of directories I need to delete.
Windows can't handle it as it's crushing before it even starts deleting. Tries Linux script but that didn't really work.
I decided to write my own program to do that. Idea is simple:
Check if there is a folder in the root path, if there is, go in it, check for folder if it's there go in and that until there is no folders, then delete all the files in that folder then delete that folder, and start again until root directory is empty.
I started trying to use OS library.
So far I got:
import os
rootdir = 'D:/TEST/'
global current_dir
current_dir = rootdir
global dir_counter
dir_counter=0
while (os.listdir(rootdir)[1]):
print(current_dir)
if(os.listdir(current_dir)[1]):
if (os.path.isdir(os.path.join(current_dir,os.listdir(current_dir)[dir_counter+1]))):
current_dir = os.path.join(current_dir,os.listdir(current_dir)[dir_counter+1])
dir_counter = dir_counter+1
I was trying just to test if it's going in directories to the end, but unfortunately it goes only one level and stays there.
My folder structure
TEST1->FOLDER->FOLDER2->FOLDER3
TEST2
TEST3
Since your need isn't really tied to python, you might consider trying some of the techniques described here:
https://superuser.com/questions/741945/delete-all-files-from-a-folder-and-its-sub-folders?answertab=votes#tab-top
Related
first post here so sorry if it's hard to understand. Is it possible to shorten the directory in python to the location of the .py file?. For example, if I wanted to grab something from the directory "C:\Users\Person\Desktop\Code\Data\test.txt", and if the .py was located in the Code folder, could I shorten it to "\data\test.txt". I'm new to python so sorry if this is something really basic and I just didn't understand it correctly.
I forgot to add i plan to use this with multiple files, for example: "\data\test.txt" and \data\test2.txt
import os
CUR_FILE = os.path.abspath(__file__)
TARGET_FILE = "./data/test.txt"
print(os.path.join(CUR_FILE, TARGET_FILE))
With this, you can move around your Code directory anywhere and not have to worry about getting the full path to the file.
Also, you can run the script from anywhere and it will work (you don't have to move to Code's location to run the script.
You can import os and get current working directory ,this will give you the location of python file and then you can add the location of folder data and the file stored in that ,code is given below
import os
path=os.getcwd()
full_path1=path+"\data\test.txt"
full_path2=path+"\data\test2.txt"
print(full_path1)
print(full_path2)
I think this will work for your case and if it doesn't work then add a comment
I'm trying to write a simple script that will return a count of files of a specific type in a folder. So far, I can get it to work for whatever folder I run it from, but I'm trying to find a way to alter it so I can drop it in a parent and return an individual count for each folder it looks at.
from pathlib import Path
import os
p = Path(Path.cwd())
p.glob('*')
files = list(p.glob('*.txt'))
number_files = len(files)
print(number_files)
exit = (input('Press any key to exit'))
This is what I've got so far. I know it works where it is because I'm pulling the current working directory, but I wanted to leave it flexible enough that I can drop it into wherever I need it and just change the filetype it's counting as I need to.
I am a beginner at Python programming (2.7; and Pygame) and I was wondering; how do you create and read files from a sub directory? In other words, I want to take sprite images, data, BGM, etc. from a sub directory named 'Data'. So, for example, if I wanted to use Pygame to open a sprite file in the sub directory 'Data', how would I do that?
What I have so far for the sprite-loading is:
char_idle = pygame.image.load("char_idle.png")
Use the os.path.join function to get the path of the file.
Add
import os
at the beginning of your code. Use it like this:
char_idle = pygame.image.load(os.path.join("Data", "char_idle.png"))
You could also provide the path as "Data/char_idle.png" (on Linux) or "Data\char_idle.png" (Windows), but it makes yout code less portable, so better use os.path.join.
I have a script that will pull files from two directories back, so the script resides at:
/folder2/folder1/folder0/script.py
and the files that will be processed will be in folder2.
I can get back one level with "..//" (I'm making a Windows executable with cx_free) but I'm thinking this isn't the best way to do this.
I am setting an input directory and an output directory. I want to keep the paths relative to the location of the script so that "folder2" can be moved without screwing up the functionality of the script or force rewriting of it.
thanks
First you get the directory where your script is located, like so:
current_dir = os.path.dirname(os.path.realpath(__file__))
And then, if you know you will always use the directory two levels above, just use:
target_dir = os.path.join(current_dir, '..', '..')
From there you can manipulate files from the target_dir as you please.
Edit
From adsmith, instead of joining two ".." paths together, you can instead define target_dir as:
target_dir = os.path.sep.join(current_dir.split(os.path.sep)[:-2])
Which will simply cut off the last two directories in your path, instead of them ending in a few uglier ".."s. So, the first method would look something like:
/path/to/folder2/folder1/directory/../..
Whereas the second implementation would be:
/path/to/folder2/
I would use your suggested method of os.chdir(r'..\..') to make sure your current working directory is in folder2. I'm not really sure what you're asking though, so maybe clarify why you think this ISN'T the right solution?
Hey. I've got a project in Python, whose directory layout is the following:
root
|-bin
|-conf
|-[project]
Python files in [project] need to be able to read configuration data from the 'conf' directory, but I cannot guarantee the location of root, plus it may be used on both Linux, Mac and Windows machines so I am trying to relatively address the conf directory from the root directory.
At the minute it's working with a dirty hack (from root/bin, particular python filename is 8 chars long):
path = os.path.abspath(__file__)[:-8]
os.chdir(path)
os.chdir("..")
[projectclass].config('config/scans.json') #for example
But this is particularly horrid and is giving me nightmares. Is there a better way to accomplish what I'm trying to achieve that doesn't feel so dirty? I feel like I'm missing something very obvious. Thanks in advance.
Instead of:
path = os.path.abspath(__file__)[:-8]
use:
path = os.path.dirname(os.path.abspath(__file__))
See the docs here.