I am trying to make a python program that creates and writes in a txt file.
the program works, but I want it to cross the "hidden" thing in the txt file's properties, so that the txt can't be seen without using the python program I made. I have no clues how to do that, please understand I am a beginner in python.
I'm not 100% sure but I don't think you can do this in Python. I'd suggest finding a simple Visual Basic script and running it from your Python file.
Assuming you mean the file-properties, where you can set a file as "hidden". Like in Windows as seen in screenshot below:
Use operating-system's command-line from Python
For example in Windows command-line attrib +h Secret_File.txt to hide a file in CMD.
import subprocess
subprocess.run(["attrib", "+h", "Secret_File.txt"])
See also:
How to execute a program or call a system command?
Directly call OS functions (Windows)
import ctypes
path = "my_hidden_file.txt"
ctypes.windll.kernel32.SetFileAttributesW(path, 2)
See also:
Hide Folders/ File with Python
Rename the file (Linux)
import os
filename = "my_hidden_file.txt"
os.rename(filename, '.'+filename) # the prefix dot means hidden in Linux
See also:
How to rename a file using Python
Related
Full Disclaimer: I DO NOT KNOW PYTHON.
Hi Guys,
I have made an AutoHotKey Script for my volume keys. I would like to create a batch file which runs a python file (so if I change computers, I can easily create this scripts) which would do the following
Check if volume_keys.ahk exists in the D Drive
If it exists, run that;
If it doesn't exist, then create a file named volume_keys.ahk and add my script to it.
My script is:
^!NumpadMult::Send {Volume_Mute}
^!NumpadAdd::Send {Volume_Up}
^!NumpadSub::Send {Volume_Down}
I know how to code the .bat file and just need help for the python point-of-view, but I request the community to check it:
#ECHO OFF
ECHO This script will run an AHK Script. If you want to stop this process from happening, then cross this window off.If you want to continye:
pause
cd d:
D:\run_volume_keys_ahk_script.py
I really appreciate any help by the community.
Thanks in advance
You can use the os library for this. Here's what the python program would look like.
import os
if os.path.isfile('D:\\volume_keys.ahk'): # check if it exists
os.system('D:\\volume_keys.ahk') # execute it
else:
with open('D:\\volume_keys.ahk', 'w') as f: # open it in w (write) mode
f.write('^!NumpadMult::Send {Volume_Mute} \
^!NumpadAdd::Send {Volume_Up} \
^!NumpadSub::Send {Volume_Down}') # Write to file
os.system('D:\\volume_keys.ahk') # execute
To activate the ahk script, you might want to use the subprocess module, of which I took the example from here
import subprocess
subprocess.call(["path/to/ahk.exe", "script.ahk"])
Note that you'll have to find the ahk executable on a computer before you can use the script, maybe you want to automatically check that too.
You can set the path you want to check for scripts in one string, and then add the filenames of your scripts as strings to a list. You can use listdir() from the os module to see any files and directories at a given path, then iterate over your scriptnames and check if it exists in that list of files. If it does, run it.
In this example I copy-pasted your script into a string as value for the key 'scriptname' in a dictionary, so that python can actually create the script file. This isn't really a neat way to do it though, you might want to have your scripts prepared in a directory next to your python script and copy them from there. See an example of how here
from os import listdir
from os.path import isfile, join
CHECK_PATH = "D:"
AHK_EXECUTABLE_PATH = "path/to/ahk.exe"
SCRIPTS_TO_CHECK = {'script1.ahk':"""^!NumpadMult::Send {Volume_Mute}
^!NumpadAdd::Send {Volume_Up}
^!NumpadSub::Send {Volume_Down} """, 'script2.ahk':" some other script here"}
files_to_check = set(listdir(CHECK_PATH)) # using a set for fast lookup later
for scriptname, script in SCRIPTS_TO_CHECK.items():
if not scriptname in files_to_check:
print(f"script {scriptname} not found, creating it.")
with open(scriptname, 'w') as file:
file.write(script)
# else
subprocess.call(AHK_EXECUTABLE_PATH, scriptname)
How can I change the display name of a file using Python?
Details:
Using Mac OS X Mojave
Python 3.7
To be precise, I want to hide the file extension of a file so that textfile.txt is seen as textfile only, without actually removing the file extension.
EDIT:
The display name of a file looks like this:
And I want it to look like this:
What you want is
import os
print(os.path.splitext("/path/to/textfile.txt")[0])
The output will be
/path/to/textfile
EDIT:
from your last edit i figured that what you're looking for is to hide extensions directly in the OS using Python. Well, this is more a task to do modifying your system settings, i don't think that you will be able to change this type of system settings directly from Python.
I have a Python tool that generates C++ files.
In order to test the tool, I have one test that compares the generated file with an expected output file.
diff = difflib.unified_diff(expectedFile.readlines(), file.readlines(), expectedFilename, filename)
The problem is that I'm getting some differences due to the format.
I can run clang-format on the expected output file.
What I'm still trying to do is to run clang-format on the generated files, just before the difflib.unified_diff is called.
Can anyone help me on how I can run clang-format in Python on a file ?
Thank you very much!
You can use the call command that is supplied by Python to call an external command. For example, you can write a script like:
#!/usr/bin/python
import sys
from subprocess import call
lc = ["clang-format","test.c"] # replace 'test.c' with the your filename.
retcode=call(lc)
sys.exit(retcode);
so i checked several other links with similar titles but, It couldn't solve my specific question. I'm trying to run a python file in notepad++ which is not a problem to me however, this file takes in a few things in order for it to compile. This is how I successfully run it in the command prompt.
python upload.py --file= "video path" --title= "title" --description= "testing"
My question is, how would i set these attributes in a different python file and then just call that file instead?
here is my code that i have in my new file
Thanks
enter image description here
enter image description here
You can use the subprocess module to do this. Following the example from the docs and the code you've listed:
import subprocess
result = subprocess.check_output('python upload.py --file="video path" --title="title" --description="testing"')
result will store any output from your command.
Note: if you're running in a windows environent, not linux, change the /usr/bin/python to python.
Maybe you can use subprocess to call your specific command.
In a separate file in the same folder, you can put a file like this:
import subprocess
subprocess.call("python upload.py --file= \"video path\" --title= \"title\" --description= \"testing\"")
And then you just call that file, and that's it...
I am trying to use Python to open a file for visual browsing, i.e. as if one double clicked a file to view it. I have tried numerous searches but because the words are very similar with doing file I/O I could not come across the appropriate information.
I think this is a very simple question / answer and I apologize if the answer was right in front of my nose.
Ideally it would just be a call on a given file path and Python would know the appropriate application to pair in case it was an extension like .pdf.
os.startfile()
os.system() but the parameters passed varies according to the OS
Windows: Start fileName
Mac: open fileName
Linux: oowriter fileName
: gnome-open fileName
: kde-open fileName etc...
Example:
fileName="file.pdf" //Path to the file
1. os.startfile(fileName)
2. os.system("start %s")%fileName
On Windows you can use os.startfile().
os.startfile("C:\\Users\\your\\file.pdf")