This question already has answers here:
What is the best way to call a script from another script? [closed]
(16 answers)
Closed 3 years ago.
I am a beginner to Python, and I would like a way to run a Python file from Python.
For example, I have a Python file named checker.py. In that file, I would like to iterate over a folder that contains inputs and outputs, and I would like to, using the Python script, give input to a different Python file, and check if it matches the expected output (in a different file). Is there any way to do this in Python?
Here is the GitHub link for the problems I have completed and need to check so far: https://github.com/vishnupsatish/CCC-practice
Try to split your big problem into several smaller ones.
First, try to find all files.
Then execute the Python files ( https://docs.python.org/3/library/subprocess.html )
Then read the output files and compare the results.
As this is a practice for you, I won't deliver all the code. Just try to do one small task after the other. And then ask when you encounter a problem.
Related
This question already has answers here:
How do I extract data from a doc/docx file using Python
(5 answers)
Closed 3 years ago.
I wrote a code that should take a file written in English and print it but instead it returned some strange things. I posted the code and an image of the output. What should i do in order to solve the problem?
f = open("Introduction-first-part.docx")
print(f.read())
Try reading in a .txt file instead. .docx is a microsoft word format which does not store text in plaintext which means when you read it you will not get 'english'. It stores things like fonts, spacing, size, and a lot of other information aside form just the contents of the file.
Copy your text out into a different format if you want to read it with python is probably the easiest way.
This question already has answers here:
How to execute a file within the Python interpreter?
(12 answers)
Closed 3 years ago.
I have file tbl.lua with lua table:
SpeedFM={}
SpeedFM[20000101]=0.77566
SpeedFM[20000102]=0.77569
SpeedFM[20000103]=0.7757
SpeedFM[20000104]=0.77569
Using lua I just make dofile('tbl.lua') and get this table to work with it.
My question. How can I execute this file in python because this is a valid python dictionary too?
In other words you are trying to execute external file within python. As long as your tbl.lua file is a valid python file then you can use one of those:
For Python 2 use execfile:
execfile('tbl.lua')
For Python 3 use exec:
exec(open("tbl.lua").read())
Be wary though as you are stepping on a really, really thin ice.
Asking yourself what you are trying to accomplish is often the most reasonable question when you are starting analyzing problem you are facing and would help you finding answers more quickly.
This question already has answers here:
How do I concatenate files in Python?
(4 answers)
Closed 7 years ago.
i have done a lot of research but i was unable to find an answer.
I have several similar CSV files without headers. I Would like to merge them but in the fastest possible way, if possible without opening the files and reading them.
I m looking for that, but in Python 2.7, the equivalen of the cmd(MS DOS) copy command:
copy *.csv output.csv
The best solution of mny problem would be some kind of command like
copy path/file1.csv .. path/file2.csv output.csv
output.csv would be the result of merging.
all csv are similar.
Thanks in advance!
The best way may just be to call the command line rather than use python's file operation functions. Check this answer for info on how to do it:
https://stackoverflow.com/a/92395/4963066
This question already has answers here:
Return values from one script to another script
(3 answers)
Closed 9 years ago.
I'm wondering if it is possible to run another file:
os.startfile('File.py')
and have that file return a value to the file that called the other file.
For example, you have File1. Is it possible for File1 to call and run File2 and have File2 return a value to File1?
Why are you running Python scripts like that? the usual way is to import one module ("another file") in a Python script and invoke the public functions from there. That's what the module importing mechanism is for, please read the linked documentation.
As mentioned in the comments, this question has been asked before. Take a look at this answer for further help.
Not with os.startfile(), no; it provides no way of communicating with the launched process. You could use the subprocess module, though; this will allow you to send data to and receive data from the launched process through standard in/out. Or, since the thing you want to call is another Python script, simply import the other file and call its functions directly, or use execfile().
This question already has answers here:
Python FTP get the most recent file by date
(5 answers)
Closed 4 years ago.
I am using Python to connect to an FTP server that contains a new list of data once every hour. I am only connecting once a day, and I only want to download the newest file in the directory. Is there a way to do this?
Seems like any system that is automatically generating a file once an hour is likely to be using an automated naming scheme. Are you over thinking the problem by asking the server for the newest file instead of more easily parsing the file names?
This wouldn't work in all cases, and if the directory got large it might become time consuming to get the file listing. But it seems likely to work in most cases.
Look at ftplib in your current version of python. You can see a function to handle the result of the LIST command that you would issue to do a dir, if you know a last time that you run a successful script then you can parse the result from the LIST and act on the new files on the directory. See the ftplib for more info on how to do it. The retrlines function is what I would expect to use.