python 能否只使用 python -m
命令参数 ,加一个文件 https://url.com/1.txt
,将这个文件下载下来?
文件下载
我猜你是不是想要wget
我猜你是想要一个执行方式和 python -m http.server 8000
一样的工具。默认的模块貌似是没有实现直接download的功能的。需要的话,可以自己实现一个放在模块目录下面,再通过 python -m download http://xxx.com/xxx
调用。
可以啊,首先说明一下,一般不需要这么费事,一个 单py文件
就可以做到,不知道你的实际用途是什么?
其次说明一下 python -m
的含义是运行一个module(也就是一个模块-可以理解为一个目录),你运行 python3 -h
可以得到如下输出:
Options and arguments (and corresponding environment variables):-b : issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors)-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x-c cmd : program passed in as string (terminates option list)-d : debug output from parser; also PYTHONDEBUG=x-E : ignore PYTHON* environment variables (such as PYTHONPATH)-h : print this help message and exit (also --help)-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x-I : isolate Python from the user's environment (implies -E and -s)-m mod : run library module as a script (terminates option list)-O : remove assert and __debug__-dependent statements; add .opt-1 before .pyc extension; also PYTHONOPTIMIZE=x-OO : do -O changes and also discard docstrings; add .opt-2 before .pyc extension-q : don't print version and copyright messages on interactive startup-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE-S : don't imply 'import site' on initialization-u : force the stdout and stderr streams to be unbuffered; this option has no effect on stdin; also PYTHONUNBUFFERED=x-v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity-V : print the Python version number and exit (also --version) when given twice, print more information about the build-W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg-x : skip first line of source, allowing use of non-Unix forms of #!cmd-X opt : set implementation-specific option. The following options are available: -X faulthandler: enable faulthandler -X showrefcount: output the total reference count and number of used memory blocks when the program finishes or after each statement in the interactive interpreter. This only works on debug builds -X tracemalloc: start tracing Python memory allocations using the tracemalloc module. By default, only the most recent frame is stored in a traceback of a trace. Use -X tracemalloc=NFRAME to start tracing with a traceback limit of NFRAME frames -X showalloccount: output the total count of allocated objects for each type when the program finishes. This only works when Python was built with COUNT_ALLOCS defined -X importtime: show how long each import takes. It shows module name, cumulative time (including nested imports) and self time (excluding nested imports). Note that its output may be broken in multi-threaded application. Typical usage is python3 -X importtime -c 'import asyncio' -X dev: enable CPython's "development mode", introducing additional runtime checks which are too expensive to be enabled by default. Effect of the developer mode: * Add default warning filter, as -W default * Install debug hooks on memory allocators: see the PyMem_SetupDebugHooks() C function * Enable the faulthandler module to dump the Python traceback on a crash * Enable asyncio debug mode * Set the dev_mode attribute of sys.flags to True * io.IOBase destructor logs close() exceptions -X utf8: enable UTF-8 mode for operating system interfaces, overriding the default locale-aware mode. -X utf8=0 explicitly disables UTF-8 mode (even when it would otherwise activate automatically) -X pycache_prefix=PATH: enable writing .pyc files to a parallel tree rooted at the given directory instead of to the code tree--check-hash-based-pycs always|default|never: control how Python invalidates hash-based .pyc filesfile : program read from script file- : program read from stdin (default; interactive mode if a tty)arg ...: arguments passed to program in sys.argv[1:]Other environment variables:PYTHONSTARTUP: file executed on interactive startup (no default)PYTHONPATH : ';'-separated list of directories prefixed to the default module search path. The result is sys.path.PYTHONHOME : alternate <prefix> directory (or <prefix>;<exec_prefix>). The default module search path uses <prefix>\python{major}{minor}.PYTHONCASEOK : ignore case in 'import' statements (Windows).PYTHONUTF8: if set to 1, enable the UTF-8 mode.PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.PYTHONHASHSEED: if this variable is set to 'random', a random value is used to seed the hashes of str and bytes objects. It can also be set to an integer in the range [0,4294967295] to get hash values with a predictable seed.PYTHONMALLOC: set the Python memory allocators and/or install debug hooks on Python memory allocators. Use PYTHONMALLOC=debug to install debug hooks.PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of locale coercion and locale compatibility warnings on stderr.PYTHONBREAKPOINT: if this variable is set to 0, it disables the default debugger. It can be set to the callable of your debugger of choice.PYTHONDEVMODE: enable the development mode.PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.
可以看到 -m mod : run library module as a script (terminates option list)
,-m
代表的意思是作为一个脚本去运行一个模块。
那么你可以在一个目录下创建一个 __main__.py
,然后这个目录就会被认为是一个模块(module),就可以通过你想要的方式去执行逻辑。
# __main__.pyimport sysprint(f"我要下载文件: {sys.argv[1]}")
然后执行命令:
python3 -m <目录名> https://url.com/1.txt
输出:
我要下载文件: https://url.com/1.txt
剩下的逻辑自己实现就行了。
问题内容: 我试图使用Python从Internet下载某些内容,但我使用的是urllib模块,但无法正常工作。我希望能够将下载的文件保存到我选择的位置。如果有人可以用清晰的例子向我解释如何做到这一点,将不胜感激。 问题答案: 我建议像这样使用urllib2: 您甚至可以将其缩短为(尽管,如果您打算将每个单独的调用括在-中,则不想将其缩短):
我正在寻找一种在python中下载pdf文件的方法,我看到了关于推荐urllib模块的其他问题的答案。我试图用它下载一个pdf文件,但是当我试图打开下载的文件时,一条消息显示该文件无法打开。 错误消息 这是我使用的代码- 我做错了什么?此外,该文件会自动保存到python文件所在的目录中。如何更改它保存到的位置? 编辑-我再次尝试使用示例pdf的链接,http://unec.edu.az/appl
运行上述代码后出现以下错误。请帮忙做这件事。。正在将源和目标文件名作为S3属性中的参数传递。。。 在处理上述异常时,发生了另一个异常: 回溯(最后一次调用):文件“C:\Cloudtail\CT\SQL Scripts\python\GRN\u S3\u dwnld.py”,第17行,除了botocore.exception.ClientErrors as e:NameError:未定义名称“bo
我正试图从一个包含阿根廷代表投票的公共网站自动下载excel文件。例如,来自以下页面:https://votaciones.hcdn.gob.ar/votacion/4108 我收到以下消息: selenium.common.exceptions。ElementClickInterceptedException:消息:元素在点(229480)处不可单击,因为另一个元素使其模糊 如果我尝试通过< c
问题内容: 我正在创建一个程序,通过读取同一游戏/应用程序的.jad文件中指定的URL,该程序将从Web服务器下载.jar(java)文件。我正在使用Python 3.2.1 我设法从JAD文件中提取JAR文件的URL(每个JAD文件都包含指向JAR文件的URL),但是正如您所想象的,提取的值是type()字符串。 相关功能如下: 但是,我总是得到一个错误,指出上面函数中的类型必须是字节,而不是字
我正在尝试下载文件从谷歌驱动器和所有我有驱动器的URL。 我读过关于google API的文章,其中谈到了一些和,这也需要一些凭据(主要是JSON)。但我不知道它是如何运作的。 此外,尝试了,但我的情况是从驱动器中获取文件。也尝试了,但没有用。 已尝试库。它有很好的上传功能驱动,但没有下载选项。 任何帮助都将不胜感激。多谢了。