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

Fix the _save_log function (sanitize filename) #19

Open
tyhallcsu opened this issue Aug 23, 2023 · 1 comment
Open

Fix the _save_log function (sanitize filename) #19

tyhallcsu opened this issue Aug 23, 2023 · 1 comment

Comments

@tyhallcsu
Copy link

My device name was:

MyName's iPhone iOS 7.1.1 (06/24/14)

This caused an issue in generating file paths with special characters and spaces in the filename.

File "/Users/tylerhall/Downloads/AIRTAGS/FindMyHistory-main/lib/log_manager.py", line 74, in _save_log
    with open(path, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: "log/2023-08-23/MyName's iPhone iOS 7.1.1 (06/24/14)__NULL.csv"

Here is my proposed fix in log_manager.py's _save_log function.

Here's the modified log_manager.py file that includes the _sanitize_filename function to handle filenames with spaces and special characters:

` def sanitize_filename(self, name):
# Replace spaces and special characters with underscores
return name.replace(' ', '
').replace('/', '').replace('\', '')

def _save_log(self, name, data):
    log_folder = self._log_folder
    if not self._no_date_folder:
        log_folder = os.path.join(
            log_folder, datetime.now().strftime(self._date_format))
    if not os.path.exists(log_folder):
        os.makedirs(log_folder)
        
    sanitized_name = self._sanitize_filename(name)
    path = os.path.join(log_folder, sanitized_name + '.csv')
    
    if not os.path.exists(path):
        with open(path, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(self._keys)
            
    with open(path, 'a') as f:
        writer = csv.writer(f)
        writer.writerow([data[k] for k in self._keys])
        `

You can add the _sanitize_filename function to your existing LogManager class to handle filename sanitization. The function replaces spaces, slashes, and backslashes in the name with underscores to create a valid filename.

This modification should help you avoid issues with special characters and spaces in the filenames when creating the CSV files. Just ensure that this updated log_manager.py file is used in conjunction with the rest of your code.

@tyhallcsu
Copy link
Author

Here is the fix:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant