Get top level folders only using Python Box SDK - python

I am trying to run a search query in Box root folder to find folder names that contain a particular string. However I only want the folders that are 1 level below (similar to a ls command). However get_items() will return folders matching the string even deeper down.
For example if I search for "AA" in the below folder structure it should only return Folder1AA, Folder2AA and Folder3AA and not Folder4AA and Folder5AA :
StartingFolder
Folder1AA
File1B
Folder4AA
Folder1C
File1D
Folder2AA
Folder5AA
File1C
Folder2B
File1D
Folder3AA
File1B
Any ideas on how to do that ?

Related

Python: Parse terminal response

I want to write a script for automatic backing up some documents on my raspberry pi to a google drive. Therefore I installed rclone and it seems to work well.
For organisation purpose I want to create for every upload a new folder with a 3 digit number e.g 001, 002, 003, ...
This is my code so far:
import os
print("Exisiting folders:")
print(os.system("rclone lsf backup_account:backup"))
print("Create new folder...")
createFolder = os.system("rclone mkdir backup_account:backup/003")
print("Exisiting folders:")
folders = str(os.system("rclone lsf backup_account:backup"))
print(type(folders))
print(len(folders))
First I print the already existing folders in the google drive directory "backup".
Second I create a new folder (in this example it is a static number and will be changed to a dynamic one, once the rest is working)
Third I print the existing folders once again to check if everything worked fine.
Up to here, everything indeed works well and i get a printout like this:
Existing folders:
001/
002/
0
Create new folder...
Existing folders:
001/
002/
003/
<type 'str'>
1
As you see, it gives the folders as a string, if i leave out the Str() it returns a int.
what I don't understand is, that the len(folders) = 1.
What I want is: Check in the beginning the existing folders and create a new one(following the numbering schema) and then copy the backup files to this new folder.
As the script wont be running all the time, i cannot store anything in a variable.
Any hints on how to put the existing folders into a list, array, ... to find the last element/highest number/...?
Running raspbian buster

Track Directory or file changes when you run the code

I created a tool that go through certain path using os.walk to check the folder if it's empty or not and list down the folders and files inside it if there is any this is how the result is
...\1.This folder\My Folder\Recored
['My Text 1.txt', 'My Text 2.txt']
OR
...\1.My Pic
This Folder is empty :(
what I want to do is track changes and color with red the new folders or the files that has been modified since the last run.
I don't want to keep watching the changes I want to see what has been change since the last run
I was trying to have something in text like log so I can compare between the current list and the text with no success
for path, directory, files in os.walk(r'C:\Users\J\MyFolder'):
if files or directory:
print(path)
print("\n")
print((os.listdir(path)))
Folder modified timestamp (os.path.getmtime("folder_path")) will provide information about folder changes timestamp value.
Your system (tool which you have created) can tell when it ran previously and also get the timestamp of folder which you are scanning, compare and color it accordingly.
To get changes in datetime format;
datetime.fromtimestamp(os.path.getmtime(folder_path))

Python how to search through all the files in a directoy

I would like to search for a word in all the files in a directory.
Eg.:
I have a folder "Test Directory" and in it I have 5 files
TestFile1.txt ... TestFile5.txt
Let's say only one of them contains a specific word Test written inside it. How can I search through all of them until I find the one with the word?
You should go with some full search engines like Elasticsearch and use python to query over Elasticsearch. But if you doing this one-time, then you take help from below code.
for file in glob.glob("/folder/path/*.txt"):
if 'word' in open(file).read():
print(file)

Moving specific files (with exceptions) from a network path

I'm trying to create a script in order to move files from a list I have. I'd like to create some conditions to that but I'm afraid that's where my Python knowledge fails me. I have a list of names (AAA, BBB, CCC).
For each of those, there are six different files with six different extensions that need to be moved (AAA.1, AAA.2, AAA.3, AAA.4, AAA.5, AAA.6). Those files might be in 3 different folders. Let's suppose, either AAA/AAA or BBB/IOL or BBB/ABC. I want all of those files to be moved to REAL/AAA. The thing is, on the folder AAA/AAA there are some AAAXXX.1 files that I do not want to be moved.
I'm completely lost and new to Python (basically, it's my first week :p).
import os
import shutil
import fnmatch
source = os.listdir(r"\\enterprise\AAA\AAA")
destination = os.listdir(r"\\enterprise\REAL\AAA")
set = {
"AAA",
"BBB",
"CCC"
}
for file in source:
for x in set:
if file.__contains__(str(x)):
print(file)
I don't know how could I specify that AAAXXX, BBBXXX and etc shall not be moved.
I don't how how to insert multiple folders for searching files with conditions (If not in folder AAA/AAA, try BBB/IOL and if not BBB/ABC)
I don't know how could I specify that AAAXXX, BBBXXX and etc shall not
be moved.
The simplest way is something like this:
if 'XXX' in file:
continue # this means skip the rest of the cycle and move to next file
I don't how how to insert multiple folders for searching files with
conditions (If not in folder AAA/AAA, try BBB/IOL and if not BBB/ABC)
The most blunt approach would be
if file_name in os.listdir('first_folder'):
# move from first
elif file_name in os.listdir('second_folder'):
# move from second
# continue adding elif for every folder
else:
print(f'file {file_name) is not found')
But I'd probably just scanned every folder and then moved everything matching given name to the destination. Although this might not be what you want if you've got duplicate names and different file contents, and you have some folder particular folder precedence.

os.listdir() is giving larger lists than expected

My book states that:
Calling os.listdir(path) will return a list of filename strings for each file in the path argument.
I tried to get the files inside a folder which is placed on the desktop and it worked perfectly fine. Then I tried to get the files in the root folder '/' and it's giving weird results.
My root folder has 5 files which include Applications, Library, Users etc but os.listdir('/') gives me a list of some 20-25 list items some of which are Applications, Library, Users,.DS_Store, Trashes, .dbfseventsd,.Spotlight-V100 etc. Note that the bold text list items do not seem to appear in the root folder when I manually open it.
Why is this happening and what should I do?
Your root folder includes hidden directories or files. These begin with a ., and are not seen by default in the Finder or ls. However, os.listdir returns them as well.
If you want to ignore these files, you may use:
files = [x for x in os.listdir('/') if not f.startswith('.')]
As an extra, it is useful to know how to view these hidden files on OSX. To see them in Finder:
Open Finder
Go to your Macintosh HD folder (access this from Devices in the left column)
Hold down CMD-Shift-. (dot)
To see them in your terminal, run ls -a /path/to/dir.

Categories