Can you link multiple Python files? - python

Just a quick question. I have been using Tkinter in Python in order to create Windows. My code is a bit all over the place when it is one file...
Is it possible to call a window that will be located in a different file?
For example,
Window1.py opens a window, there is a button in that window that should initiate window 2, which is located in Window2.py. Does the code physically have to be in the same file for it to work together?

Yes you can do it:
import os
#executing the python file within the first using this command>>
os.system("python fle_name.py)

The answer to this question is yes.
To link two python files, use:
If you are in python 3, use exec(open(r"example").read())
If you are in python 2, use open(r"example")
-
Note: the python 2 example works both in python 2 and 3
-
They do not need to be in the same file, just simply use their location.
e.g. if I had a program on my desktop, I would use
exec(open(r"C:/Users/MyName/Desktop/program").read())

Related

how do I track changes in file using python

suppose the below text is present in the file
1.a=2
2.b=3
3.print(a+b)
if i change the value of b to 5, how to print that in this line this field is changed in python
I think you could use gif for version control of your source code and some library like GitPython to be able to interact from python.

how to modify txt file properties with python

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

Find desktop folder in a custom location? [duplicate]

I have this small program and it needs to create a small .txt file in their 'My Documents' Folder. Here's the code I have for that:
textfile=open('C:\Users\MYNAME\Documents','w')
lines=['stuff goes here']
textfile.writelines(lines)
textfile.close()
The problem is that if other people use it, how do I change the MYNAME to their account name?
Use os.path.expanduser(path), see http://docs.python.org/library/os.path.html
e.g. expanduser('~/filename')
This works on both Unix and Windows, according to the docs.
Edit: forward slash due to Sven's comment.
This works without any extra libs:
import ctypes.wintypes
CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
print(buf.value)
Also works if documents location and/or default save location is changed by user.
On Windows, you can use something similar what is shown in the accepted answer to the question: Python, get windows special folders for currently logged-in user.
For the My Documents folder path, useshellcon.CSIDL_PERSONALin the shell.SHGetFolderPath() function call instead of shellcon.CSIDL_MYPICTURES.
So, assuming you have the PyWin32 extensions1 installed, this might work (see caveat in Update section below):
>>> from win32com.shell import shell, shellcon
>>> shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0)
u'<path\\to\\folder>'
Update: I just read something that said that CSIDL_PERSONAL won't return the correct folder if the user has changed the default save folder in the Win7 Documents library. This is referring to what you can do in library's Properties dialog:
The checkmark means that the path is set as the default save location.
I currently am unware of a way to call the SHLoadLibraryFromKnownFolder() function through PyWin32 (there currently isn't a shell.SHLoadLibraryFromKnownFolder. However it should be possible to do so using the ctypes module.
1Installers for the latest versions of the Python for Windows Extensions are currently available from: http://sourceforge.net/projects/pywin32

Accessing a python script in a different folder, using a python script

Can a python script located in D:\Folder_A\Folder_B\be used to change variables in another python script located in D:\Folder_A\Folder_C\, and then print them out on the console?
NOTE: I'm a beginner in Python, and I'm using v2.7.8.
EDIT: To answer #Rik Verbeek, I created a python file in location A (D:\Folder_A\Folder_B\), and another file at location B (D:\Folder_A\Folder_C\), with both the folders (Folder_B & Folder_C) located in D:\Folder_A\. In the 2nd .py file, I wrote the following:
a = 0
b = 0
Now I want to change these variables from the 1st .py file to 5 and 10 respectively, and print them to the console. Also, these files are not in the Python libraries, but are instead, located in another folder(Folder_A).
To answer #Kasra (and maybe #cdarke), I don't want to import a module, unless it is the only way.
If you have some "global" variables I think is a good idea to have them in a separated module and import that module from each place you need them. This way you only have to do it as cdarke has commented.

How to use the Windows Explorer or Finder file dialogues in Tkinter/Python2.7.3?

Among other things, I am currently trying to create a basic text editor which can open text files, edit them, and then save them. I have used this Tkinter dialogue for the GUI 'file manager,' but I was wondering if anyone knew the way to access the one that comes default on Windows?
Thanks!
Technical Things:
OS: Windows 7
Language: Python 2.7.3
EDIT 1
By the DEFAULT file dialogue, I mean the windows explorer dialogue:
I also use mac. Assuming that my application is cross-platform, would there be any way for me to have the program check what the os was, and then open either Finder or Windows Explorer.
I need the program to be able to save and open items in different commands. How would I do this?
It's not exactly clear what you're asking, since the one that tkinter comes with is default in Windows. Here's another link for that, just in case you got mixed up somewhere along the line. Remember that you can set it so it only finds a certain type of file, starts in a specific place, returns the filename or directory, or even open the file (I think)
If you mean the Windows Explorer you can open it and close it with pywin32, but not much else. Taken from this answer
import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
try importing tkFileDialog:
import tkFileDialog as tkfd
def save():
savenm = tkfd.asksaveasfile()
f = open(savenm.name,"w")
# then put what to do with the opened file
def open():
opennm = tkfd.askopenfile()
f = open(savenm.name,"r")
# then put what to do with the opened file
then make a button that uses the functions:
import Tkinter as tk
root=tk.Tk()
SAVELOADFRAME = tk.Frame(root)
SAVELOADFRAME.pack()
savebtn = Button(SAVELOADFRAME,text="Save",command=save)
savebtn.pack(side=LEFT)
root.mainloop()
loadbtn = Button(SAVELOADFRAME,text="Open",command=open)
loadbtn.pack(side=RIGHT)
maybe if you have a notepad box you might want to insert the text from the file into the tk.Text widget. The above code only works for text based files really (e.g. *.js, *.txt, *.py) not *.exe, *.dll, etcetera.
hope that solves your problem :^)

Categories