-
Notifications
You must be signed in to change notification settings - Fork 489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script and description added #1393
Open
TheBugYouCantFix
wants to merge
2
commits into
HarshCasper:master
Choose a base branch
from
TheBugYouCantFix:file_sort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# File sort | ||
|
||
## Description | ||
This is a simple script that sorts the files from a given directory by their extensions. For example, all the .png files will be put to the png folder and so on. | ||
The script will require a user to provide a path to directory that is going to be sorted. | ||
|
||
## Modules | ||
* os | ||
* shutil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
import shutil | ||
|
||
DIR_NAME = 'sorted' | ||
source_path = input("Please input the directory name to be sorted: ") | ||
|
||
if source_path == '.': | ||
source_path = os.getcwd() | ||
|
||
path = os.path.join(source_path, DIR_NAME) | ||
print(source_path, path) | ||
|
||
if not os.path.exists(path): | ||
os.mkdir(path) | ||
|
||
os.chdir(path) | ||
files = [] | ||
|
||
|
||
def get_files(): | ||
"""getting all the files that have been put to the sorting folder by user and | ||
extending them to our files list to sort them in the future""" | ||
|
||
files.extend(list(os.walk(source_path))[0][2]) # extending the list of all the files in a source path | ||
|
||
|
||
def sort_files(clear_files=True) -> int: | ||
get_files() | ||
|
||
n_of_sorted_files = 0 # keeps track of the sorted files | ||
to_remove = [] # transferred files can be removed(from files list) only after the for loop ends bc we're | ||
# iterating through this list -> the list can't be changed during the iteration | ||
|
||
for file in files: | ||
|
||
extension = file.split('.')[-1] # get the extension of the current file | ||
current_file_path = os.path.join(source_path, file) # putting the file into out sorting folder | ||
new_folder = os.path.join(path, extension) # declares a new folder variable with the extension name | ||
|
||
# Creating this folder if it hasn't already been created | ||
if not os.path.exists(new_folder): | ||
os.mkdir(new_folder) | ||
|
||
shutil.copy(current_file_path, new_folder) # putting a file to the folder with its extension | ||
|
||
if clear_files: | ||
os.remove(current_file_path) # removing the file from the sorting folder | ||
|
||
to_remove.append(file) # appending sorted files to remove them later | ||
|
||
# removing files that are already sorted from our files list | ||
for i in to_remove: | ||
files.remove(i) | ||
|
||
n_of_sorted_files += len(to_remove) # increasing the counter of sorted files | ||
|
||
return n_of_sorted_files | ||
|
||
|
||
def main(): | ||
counter = 0 # counts an amount of transferred files | ||
|
||
counter += sort_files() | ||
print(f'Number of sorted files: {counter}') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has problems when the input is a
.
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just pushed a new commit fixing this issue