Skip to content
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

Command Line Argument #301

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions CommandLineArgument/Argument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import argparse
def open_file():
try:
file_path = input("Enter the path to the file: ")
os.startfile(file_path)
except Exception as e:
print(f"Error opening file: {e}")






Comment on lines +8 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"Error opening file: {e}")
print(f"Error opening file: {e}")

def create_folder():
folder_path = input("Enter the path to the folder: ")
folder_name = input("Enter the name of the folder: ")
try:
os.makedirs(os.path.join(folder_path, folder_name), exist_ok=True)
print(f"Folder '{folder_name}' created successfully.")
except Exception as e:
print(f"Error creating folder: {e}")


def write_to_file():
file_path = input("Enter the name of the file: ")
file_name = os.path.basename(file_path)
try:
with open(file_path, 'w') as f:
f.write(input("Enter the text to be written to the file: "))
print(f"File '{file_name}' created successfully.")
except Exception as e:
print(f"Error writing to file: {e}")


def main():

parseer = argparse.ArgumentParser(description="Command Line Argumet")
parseer.add_argument("-w", "--write", help="write to a file", action="store_true")
parseer.add_argument("-o", "--open", help="open a file", action="store_true")
parseer.add_argument("-c", "--create", help="create a new folder", action="store_true")
GhalibFaruqe101 marked this conversation as resolved.
Show resolved Hide resolved
args = parseer.parse_args()
if args.write:
write_to_file()
elif args.open:
open_file()
elif args.create:
create_folder()


Comment on lines +49 to +50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

else:
print("Invalid input. Please try again.")

if __name__== "__main__":
main()
31 changes: 31 additions & 0 deletions CommandLineArgument/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# File Management Tool

A simple command-line tool for managing files and folders in Python. This tool allows users to create folders, open files, and write content to files through command-line arguments.

## Features

- **Open a File**: Opens a specified file using the default application.
- **Create a Folder**: Creates a new folder at a specified location.
- **Write to a File**: Creates a new file or overwrites an existing one with user-provided text.

## Requirements

- Python 3.x
- os
- argparse

## Usage

### Command Line Arguments

You can use the following command-line arguments to execute different functionalities:

- `-o` or `--open`: Opens a specified file.
- `-c` or `--create`: Creates a new folder.
- `-w` or `--write`: Writes to a specified file.

### Examples

1. **To Open a File:**
```bash
name_of_the_file -o
1 change: 1 addition & 0 deletions CommandLineArgument/run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Yeah!!
Binary file not shown.