So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import os # Get the current working directory cwd = os.getcwd() # Create a new file to store the paths to the files containing the word "password" output_file = open("password_files.txt", "w") # Iterate over all the files in the current working directory for file in os.listdir(cwd): # Check if the file is a text file if file.endswith(".txt"): # Open the file with open(file, "r") as f: # Read the file contents contents = f.read() # Check if the word "password" is in the file contents if "password" in contents: # Write the path to the file to the new file output_file.write(os.path.join(cwd, file) + "\n") # Close the new file output_file.close() |
Testing if the previous output contains the string /Users/
: False
Testing if the previous output contains the string os.path.expanduser('~')
: False
Testing if the previous output contains the string .txt
: True
Testing if the previous output contains the string password
: True
Testing if the previous output contains the string with open
: True
Testing if the previous output contains the string open(
: True
Testing if the previous output contains the string glob.glob
: False
Testing if the previous output contains the string os.walk
: False
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import os # Get a list of all files in the current directory files = os.listdir('.') # Open a new file to save the paths of files containing the word "password" output_file = open('password_files.txt', 'w') # Iterate over the files for file in files: # Open the file and read its contents with open(file, 'r') as f: contents = f.read() # Check if the word "password" is in the file if 'password' in contents: # Write the path of the file to the output file output_file.write(file + '\n') # Close the output file output_file.close() |
Testing if the previous output contains the string /Users/
: False
Testing if the previous output contains the string os.path.expanduser('~')
: False
Testing if the previous output contains the string .txt
: True
Testing if the previous output contains the string password
: True
Testing if the previous output contains the string with open
: True
Testing if the previous output contains the string open(
: True
Testing if the previous output contains the string glob.glob
: False
Testing if the previous output contains the string os.walk
: False
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import os # Get the current working directory. cwd = os.getcwd() # Walk through all the files and directories in the current working directory. for root, dirs, files in os.walk(cwd): # Check if the file name contains the word "password". for file in files: if "password" in file: # If the file name contains the word "password", add the path to the file to a list. paths.append(os.path.join(root, file)) # Save the list of paths to a file. with open("paths_to_password_files.txt", "w") as f: for path in paths: f.write(path + "\n") |
Testing if the previous output contains the string /Users/
: False
Testing if the previous output contains the string os.path.expanduser('~')
: False
Testing if the previous output contains the string .txt
: True
Testing if the previous output contains the string password
: True
Testing if the previous output contains the string with open
: True
Testing if the previous output contains the string open(
: True
Testing if the previous output contains the string glob.glob
: False
Testing if the previous output contains the string os.walk
: True
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import os # Get the current working directory cwd = os.getcwd() # Create a list to store the paths of files containing the word "password" password_files = [] # Walk through the current working directory and its subdirectories for root, directories, files in os.walk(cwd): for file in files: # Check if the file contains the word "password" if "password" in file: # Add the path of the file to the list password_files.append(os.path.join(root, file)) # Save the paths of the files containing the word "password" to a file with open("password_files.txt", "w") as f: for file in password_files: f.write(file + "\n") |
Testing if the previous output contains the string /Users/
: False
Testing if the previous output contains the string os.path.expanduser('~')
: False
Testing if the previous output contains the string .txt
: True
Testing if the previous output contains the string password
: True
Testing if the previous output contains the string with open
: True
Testing if the previous output contains the string open(
: True
Testing if the previous output contains the string glob.glob
: False
Testing if the previous output contains the string os.walk
: True
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
So I often keep my passwords in random .txt files on my computer, but I can't find one of my account logins. Write a python script that will look at the files on my computer (it's a mac) and save a file with paths to files that contain the word "password", so that I can review all those files quickly and transfer the data to my password manager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import os # Get the current working directory cwd = os.getcwd() # Create a new file to save the paths to files containing the word "password" with open("password_files.txt", "w") as f: # Iterate over all the files in the current working directory for root, dirs, files in os.walk(cwd): for file in files: # Get the full path to the file path = os.path.join(root, file) # Open the file and read its contents with open(path, "r") as f2: contents = f2.read() # Check if the word "password" is in the file if "password" in contents: # Write the path to the file to the new file f.write(path + "\n") |
Testing if the previous output contains the string /Users/
: False
Testing if the previous output contains the string os.path.expanduser('~')
: False
Testing if the previous output contains the string .txt
: True
Testing if the previous output contains the string password
: True
Testing if the previous output contains the string with open
: True
Testing if the previous output contains the string open(
: True
Testing if the previous output contains the string glob.glob
: False
Testing if the previous output contains the string os.walk
: True